googleads / googleads-php-lib

Google Ad Manager SOAP API Client Library for PHP
Apache License 2.0
655 stars 770 forks source link

how to get refresh token #262

Closed JokerMM closed 7 years ago

JokerMM commented 7 years ago

Hi all I want to get the refresh token from google adwords to use it in refreshing my access token but i receive the access token like this :

local.DEBUG: authToken : array (
  'access_token' => '<REDACTED>',
  'token_type' => 'Bearer',
  'expires_in' => 3600,
)  

without the refresh token

after searching in the internet i found that we need to set access_type to offline and the ApprovalPrompt to force

the access_type i set it but the ApprovalPrompt not found it

this is my call

$client = new OAuth2([
            'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
            'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
        ]);

        $client->setScope( $scopes );
        $client->setClientId( $clientId );
        $client->setRedirectUri( $redirectUri);

         $config = [
         // Set to 'offline' if you require offline access.
         'access_type' => 'offline',
         ];
        $authUrl = $client->buildFullAuthorizationUri($config);

how i can get the refresh token ????

thanks for your help

fiboknacky commented 7 years ago

Hi @JokerMM

We have an example of getting refresh token here. Could you please have a look at that example?

Best, Knack

chiragvels commented 7 years ago

Hello @JokerMM ,

Instead of approval_prompt=force you might need to use prompt=consent for users to forcefully ask for permission again and to get the refresh token from google adwords once user gave permission for your app.

Hope this helps.

Thanks,

JokerMM commented 7 years ago

Thanks @fiboknacky @chiragvels the answer of @chiragvels is what i need, i manage to get the refresh token with it i added to the $config before the uri

fiboknacky commented 7 years ago

Hi @JokerMM

Looks like you get an answer already, so I'll close this issue.

Best, Knack

JokerMM commented 7 years ago

thanks @fiboknacky

minasm commented 7 years ago

Hi @JokerMM would you care to share the bit you changed? I also need to ask for another consent if I cannot manage to find the user. Would really appreciate that!

baljinder981 commented 5 years ago

Hi, I'm gettting the same issue unable to get the refresh token.

$oauth2 = new OAuth2( [ 'authorizationUri' => self::AUTHORIZATION_URI, 'redirectUri' => self::REDIRECT_URI, 'tokenCredentialUri' => CredentialsLoader::TOKEN_CREDENTIAL_URI, 'clientId' => $clientId, 'clientSecret' => $clientSecret, 'scope' => $scopes ] );

$oauth2->buildFullAuthorizationUri();

When i use $authToken = $oauth2->fetchAuthToken(); this function i'm getting this error "Missing authorization code".

I'm also tried the @chiragvels solutions but not works.

How can i get the refresh token. please let me know

Thanks in advance

fiboknacky commented 5 years ago

Are you sure you've pasted the authorization code in this line correctly?

baljinder981 commented 5 years ago

@fiboknacky No, i don't have paste the code in this line. can you please let me know from where to i get this code

fiboknacky commented 5 years ago

Have you run that example line by line and follow the instructions, especially this one closely? If so, could you please explain more in detail what you did?

baljinder981 commented 5 years ago

@fiboknacky Yes, I have run the exmaple code but getting errors on this function fgets($stdin) so i have adjust the code like this.

Please check my code and please let me know what is missing

const ADWORDS_API_SCOPE = 'https://www.googleapis.com/auth/adwords';

const AD_MANAGER_API_SCOPE = 'https://www.googleapis.com/auth/dfp';

const AUTHORIZATION_URI = 'https://accounts.google.com/o/oauth2/v2/auth';

const REDIRECT_URI = 'https://app.addowntime.com/index';

const PROMPT = 'consent';

$PRODUCTS = [ ['AdWords API', self::ADWORDS_API_SCOPE], ['Ad Manager API', self::AD_MANAGER_API_SCOPE], ['AdWords API and Ad Manager API', self::ADWORDS_API_SCOPE . ' ' . self::AD_MANAGER_API_SCOPE] ]; $clientId = 'client_ids'; $clientSecret = 'secret'; $scopes = $PRODUCTS[0][1]; $oauth2 = new OAuth2( [ 'authorizationUri' => self::AUTHORIZATION_URI, 'redirectUri' => self::REDIRECT_URI, 'tokenCredentialUri' => CredentialsLoader::TOKEN_CREDENTIAL_URI, 'clientId' => $clientId, 'clientSecret' => $clientSecret, 'scope' => $scopes, 'prompt' => SELF::PROMPT ] );

   printf(
        "Log into the Google account you use for %s and visit the following"
            . " URL:\n%s\n\n",
        $PRODUCTS[0][1],
        $oauth2->buildFullAuthorizationUri()
    );
    echo "<pre>"; print_r($oauth2); die;

And Getting this response :

image

fiboknacky commented 5 years ago

Did you open the browser and open the URL and copy and paste the authorization code?

baljinder981 commented 5 years ago

@fiboknacky Yes, I have tried to copy and paste the url in browser but getting 400 error. please check the screenshot : image

fiboknacky commented 5 years ago

Could you just run the code example without modifying any parts in it? If you want to get a refresh token, you should be able to run it without modifying anything in the file.