googleads-php-lib icon indicating copy to clipboard operation
googleads-php-lib copied to clipboard

API access on behalf of your clients (web flow) can't refresh properly

Open steponafish opened this issue 8 years ago • 11 comments

I'm using the API Access on behalf of my clients web flow and I am able to successfully pull the example code CampaignService in the example. The only thing I can't figure out is that the State is still in the URL after being redirected and that when I refresh, the web page breaks and the token never gets reused.

I have the oauth code placed above the html tag to get the refresh token and then it pulls up the page, but I can't figure out how to save the token properly.

steponafish avatar May 07 '18 23:05 steponafish

Have you tried these steps yet?

fiboknacky avatar May 08 '18 02:05 fiboknacky

I have that piece of code already. I wasn't sure what to replace with the "..." though. On the page that is running this, I'm requiring both the autoload file and this new oauth2 instance.

steponafish avatar May 08 '18 15:05 steponafish

So could you share your code snippet here then? Thanks.

fiboknacky avatar May 08 '18 15:05 fiboknacky

screenshot

steponafish avatar May 08 '18 15:05 steponafish

So, could you clarify what's the purpose of your app? Do you want to use refresh token (so you don't need to ask your user to authorize again) or do you want to ask the user to authorize every time?

If the former, then following this guide would be more appropriate.

If the latter, then what you need to do in the else clause is to use our client library to issue requests to the API server, as usual. For example, you would use $oauth2 to build AdWordsSession:

$session = (new AdWordsSessionBuilder())
    ->fromFile()
    ->withOAuth2Credential($oauth2)
    ->build();

$adWordsServices = new AdWordsServices();
$campaignService = $adWordsServices->get($session, CampaignService::class);
// Make calls using $campaignService.

fiboknacky avatar May 09 '18 10:05 fiboknacky

I'm trying to build out a customized dashboard to show a few SEM modules from AdWords. I would like to have a refresh token be asked once and not every time they refresh the page.

I have the included code that you have from the previous response, is that why I'm being asked every time?

steponafish avatar May 09 '18 15:05 steponafish

I have an MCC account and I thought the webflow to access on behalf of the client would be the better route. Is this the better webflow for that?

steponafish avatar May 09 '18 15:05 steponafish

If I use the installed application flow, I get Unauthorized Client Error.

steponafish avatar May 09 '18 15:05 steponafish

If you want to store the refresh token, then in Step 2.2, you need to use offline instead of online:

 $config = [
    // Set to 'offline' if you require offline access.
    'access_type' => 'offline'
  ];

Then in step 2.3, you will get $refreshToken for a specific customer account:

$refreshToken = $authToken['refresh_token'];

You need to store that securely, so next time, for the specific customer account, you can use refresh token to issue API requests on behalf of the customer.

fiboknacky avatar May 09 '18 15:05 fiboknacky

Having said that, installed application flow should also work too. I suggest you to review this guide thoroughly.

For the error regarding OAuth2 installed application flow, please reach out to AdWords API Forum.

fiboknacky avatar May 09 '18 15:05 fiboknacky

Closing this due to inactivity.

fiboknacky avatar May 31 '18 07:05 fiboknacky