php 谷歌广告 api - unauthorized_client

标签 php google-ads-api google-client

我正在使用“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/

相关文章:

java - 如何从 Google Sheets v4 API 获取排序的单元格数据?

c# - 无法将文件上传到谷歌驱动器 - 使用 C#

php - 在 PDO 中动态使用数组值插入值

java - 模拟广告词 API

php - 使用 PHP 显示表中的记录

admob - 任何允许 AJAX 请求的移动广告平台?

php - 跟踪用户点击返回 Google Adwords API

javascript - OAuth Google Analytics 客户端 API javascript

php - 语音识别 : is possible in PHP or not?

php - Laravel 登录后重定向