最新的Google Adwords oAuth API实现



美好的一天,

我在尝试将我的 adwords API oAuth 迁移到较新的 API 版本(即 v201705(时遇到问题。

我使用 v201609 的旧实现正在工作,现在由于这个版本已经日落了。我一直在将我的应用程序迁移到更新的 API 版本。

基本上,我很难从Google或任何地方找到任何文档,这些文档显示了如何使用与最新Adwords API版本相关的Adwords API实现Google oAuth。

以下是我当前的代码:

$redirectUri = 'http://xxxxxxx/dashboard/accounts/oauth2callback';
$code = $_GET['code'];
//$code = Request::query('code');
$user = new AdWordsUser(
base_path('auth.ini'),
null,
null,
null,
base_path('settings.ini'),
null
);
$OAuth2Handler = $user->GetOAuth2Handler();
// Get the access token using the authorization code. Ensure you use the same
// redirect URL used when requesting authorization.
$user->SetOAuth2Info(
$OAuth2Handler->GetAccessToken(
$user->GetOAuth2Info(), $code, $redirectUri));
// The access token expires but the refresh token obtained for offline use
// doesn't, and should be stored for later use.
//return $user->GetOAuth2Info();
//$result = json_decode($user->GetOAuth2Info());
$refreshToken = $user->GetOAuth2Info()['refresh_token'];

谁能指出我最新的Google Adwords API版本的正确实现?

以下是我目前一直在努力实现的目标,但显然这不起作用和/或完全缺少某些东西:

$redirectUri = 'http://xxxxxxx/dashboard/accounts/oauth2callback';
$code = $_GET['code'];
//$code = Request::query('code');
/* $user = new AdWordsUser(
base_path('auth.ini'),
null,
null,
null,
base_path('settings.ini'),
null
); */
// Generate a refreshable OAuth2 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())
->fromFile()
->build();
// Construct an API session configured from a properties file and the OAuth2
// credentials above.
$user = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
$OAuth2Handler = $user->GetOAuth2Handler();
// Get the access token using the authorization code. Ensure you use the same
// redirect URL used when requesting authorization.
$user->SetOAuth2Info(
$OAuth2Handler->GetAccessToken(
$user->GetOAuth2Info(), $code, $redirectUri));
// The access token expires but the refresh token obtained for offline use
// doesn't, and should be stored for later use.
//return $user->GetOAuth2Info();
//$result = json_decode($user->GetOAuth2Info());
$refreshToken = $user->GetOAuth2Info()['refresh_token'];
$customer_id = $this->GetClientCustomerName($refreshToken)['customer_id'];
//$customer_name = $this->GetClientCustomerName($refreshToken)['customer_name'];

谢谢。

好吧,我终于发现我需要遵循Google OAuth2实现,就像我之前使用Google Adwords实现一样。不知道两者都会一样工作。

以下是我提出并正在工作的内容:

$redirectUri = 'http://xxxxxxx/dashboard/accounts/oauth2callback';
$code = $_GET['code'];
$oauth2 = new OAuth2([
'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
'redirectUri' => $redirectUri,
'clientId' => $this->client_id,
'clientSecret' => $this->client_secret,
'scope' => '****'
]);

$oauth2->setCode($code);
$authToken = $oauth2->fetchAuthToken();
// Store the refresh token for your user in your local storage if you
// requested offline access.
$refreshToken = $authToken['refresh_token'];

最新更新