duankui6150 2015-12-22 20:24
浏览 45
已采纳

Google Adwords API。 如何使用PHP库刷新令牌

Let me start here. The Google Adwords API does not work with the standard Google API Oauth method. It has it's own. Please do not respond with using that library or it's methods it is different.

OK, over the last couple days I have been integrating the GoogleAdwords API OAuth method code into my companies code. I can get an access and refresh token from the server with no issue. The access tokens are good for 1 hours and then need to be refreshed.

// oauth 2 config
$credentials= array(
    'client_id' => $config['id'],
    'client_secret' => $config['secret'],
    'access_token' => $params['access_token'],
    'refresh_token' => $params['refresh_token']
);

$user = new \AdWordsUser(null, $adwords['dev_token'],
    $adwords['useragent'], null, null, $credentials);

The above code works properly with a brand new token. If I take the $user object and try to get CustomerService object from the API I can get the data back. However after an hour the token goes bad and I am no longer able to use it.

I have searched Google's Adwords API Forum (via Google Groups), and also a general Google search. The only code I have been able to find is this code.

$client = $user->GetOAuth2Handler();
$credentials = $client->RefreshAccessToken($user->GetOAuth2Info());
$user->SetOAuth2Info($credentials);

But when I add it I always get this error.

{ "error" : "unauthorized_client" }

I am at a complete loss...

Also please not I do not use an auth.ini we use our own config files and add the parameters to the api objects.

  • 写回答

1条回答 默认 最新

  • doula2426 2015-12-23 03:28
    关注
    { "error" : "unauthorized_client" }
    

    This error means that a different google application client_id was specified. We use multiple google apps and I had copied and pasted some code forgetting that for Adwords we are using a new app, where other google APIs we integrate are all using the same app.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?