gpt4 book ai didi

php 谷歌广告 api - unauthorized_client

转载 作者:行者123 更新时间:2023-12-05 07:42:30 26 4
gpt4 key购买 nike

我正在使用“Google_Client”类来验证对 adwords api 的访问权限,因此:

$REDIRECT_URI = 'http://localhost/GoogleAdwords/auth.php';
$client = new Google_Client();
$client->setApplicationName("App name");

$SCOPES = array('https://www.googleapis.com/auth/adwords'
);
$client->setIncludeGrantedScopes(true);
$client->setAccessType('offline');
$client->setRedirectUri($REDIRECT_URI);
$client->setScopes($SCOPES);

try{
$oauth2 = (new OAuth2TokenBuilder())
->withClientId($OAuthCredentials['clientId'])
->withClientSecret($OAuthCredentials['clientSecret'])
->withRefreshToken($client->getRefreshToken())
->build();

}catch(Exception $err){
//redirect("auth.php");
echo $err;
}

认证后:

$oauth2 = (new OAuth2TokenBuilder())
->withClientId($OAuthCredentials['clientId'])
->withClientSecret($OAuthCredentials['clientSecret'])
->withRefreshToken($client->getRefreshToken())
->build();
$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oauth2)
->build();

$adWordsServices = new AdWordsServices();
$managedCustomerService = $adWordsServices->get($session, ManagedCustomerService::class);



$selector = new Selector();
$selector->setFields(['CustomerId', 'Name']);
$selector->setOrdering([new OrderBy('CustomerId')]);


try{
$mccAccounts = $managedCustomerService->get($selector);
}catch(Exchange $e){
// echo $e->getMessage();
}

我很确定客户端已通过身份验证,因为我可以获得 refreshToken 和访问 token 。但是我收到了这个错误:

 Client error: `POST https://www.googleapis.com/oauth2/v4/token` resulted in a `401 Unauthorized` response: { "error": "unauthorized_client", "error_description": "Unauthorized" } 

这可能是什么问题?我错过了什么吗?

编辑:

使用相同的 $client 访问用户信息和购物内容效果很好,但对于 adwords 则不然。

最佳答案

这对我有用,但我不太了解 Google 的库,无法判断是否有必要,也许其他人可以减少脂肪。

$client = new \Google_Client();
$client->setApplicationName('NAME_HERE');
$client->setAuthConfig(__DIR__ . '/../Credentials/oauth.json');
$client->setAccessType('offline');
$client->setApiFormatV2(2);
//// Load previously authorized credentials from a file.
$oAuth = json_decode(file_get_contents(__DIR__ . '/../Credentials/oauth.json'), true);
$accessToken = json_decode(file_get_contents(__DIR__ . '/../Credentials/token.json'), true);
$client->setAccessToken($accessToken);

$oauth2 = (new OAuth2TokenBuilder())
->withClientId($oAuth['installed']['client_id'])
->withClientSecret($oAuth['installed']['client_secret'])
->withRefreshToken($client->getRefreshToken())
->build();

$session = (new AdWordsSessionBuilder())
->withDeveloperToken('TOKEN_HERE')
->withClientCustomerId("CUSTOMER_ID_HERE")
->withOAuth2Credential($oauth2)
->build();

$adWordsServices = new AdWordsServices();
$managedCustomerService = $adWordsServices->get($session, ManagedCustomerService::class);

$selector = new Selector();
$selector->setFields(['CustomerId', 'Name']);
$selector->setOrdering([new OrderBy('CustomerId')]);
$mccAccounts = $managedCustomerService->get($selector);

dd($mccAccounts) ;

关于php 谷歌广告 api - unauthorized_client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44468488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com