0

I am working in an Angular app, I want to add Gmail in this Angular app.

I have followed this tutorial https://medium.com/@mcflyDev/angular-2-or-4-import-google-contacts-d0ffb13d8626

everything is working fine and I am getting the success response with access_token but I am not getting authorization_code.

How can I get authorization_code?

Here is my configuration:

     this.auth2 = gapi.auth2.init({
        client_id: 'bla-bla-bla-2.apps.googleusercontent.com',
        cookiepolicy: 'single_host_origin',
        scope: 'https://www.googleapis.com/auth/gmail.readonly',
        access_type: 'offline',
        response_type: 'code',
        auth_uri: 'https://accounts.google.com/o/oauth2/v2/auth',
        prompt: 'select_account',
        include_granted_scopes: true,
        grant_type: 'authorization_code',
      });

Also, I am also not getting refresh_token, as you can see I have already set access_type: 'offline'.

enter image description here

I am getting this response as shown in the above image.

Thanks.

2
  • Hi @Arpit Meena did you find the answer? A have the same problem. Commented Nov 13, 2018 at 8:25
  • @pelcomppl, are you making POST Ajax call? Commented Nov 13, 2018 at 8:59

3 Answers 3

1

I got the solution. Earlier I was making a POST Ajax call but that was not required.

We just need to do a small setup in 2 steps:

Step 1. Prepare a URL:

The URL should look like this:

 https://accounts.google.com/o/oauth2/auth?redirect_uri=:your_redirect_url&response_type=code&client_id=:your_client_id&scope=https://www.googleapis.com/auth/gmail.send&approval_prompt=force&access_type=offline

You can paste this URL in your browser and hit enter to check it is working.

Step 2. Create an anchor tag in HTML:

<a href="giveAboveURLHere">Connect With Gmail</a>

Congratulations!!! You have done.

Whenever user will click on this link, Google will ask for permission for the provided scope and if the access is granted by the end user then Google will redirect to the given redirect_url with the authorization_code in the query param.

Then on the server side, you will need to make another API call with this authorization_code then Google will provide access_token and refresh_token.

Thanks, I hope it will help.

Sign up to request clarification or add additional context in comments.

Comments

0

@Arpit Meena I found another solution:

On button "Connect With Gmail" click I run this.auth2.grantOfflineAccess().

It fires google account choose popup and immediately after that popup to grant privileges to scopes. It returns authorization_code which I can use to get refresh_token and access_token in my API ($gClient->authenticate($code);).

Comments

0

This is the one i found best to get auth code by use of js client library. rest on g-site only http/rest method is mentioned.

function initClient() {
            gapi.client.init({
              apiKey: API_KEY,
              clientId: CLIENT_ID,
              discoveryDocs: DISCOVERY_DOCS,
              scope: SCOPES,
              access_type:'offline',
              include_granted_scopes: true
            }).then(function () {
         
              offinebutton.onclick = offlinefunction;
              
            }, function(error) {
              appendPre(JSON.stringify(error, null, 2));
            });
          }

    function offlinefunction() {
            gapi.auth2.getAuthInstance().grantOfflineAccess().then(function(resp) {
            console.log(resp.code)
            });
          }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.