setAssertionCredentials on google-api-php-client 2.0

本小妞迷上赌 提交于 2019-12-07 19:37:00

问题


I updated from 1.1.5 to 2.0 and I cannot get it to login. I followed the UPGRADE.md instructions, on how to upgrade from using "setAssertionCredentials", but I can't get it to work.

BEFORE:

$client = new Google_Client();
$client->setApplicationName('API Project');
$client->setAssertionCredentials(
    new Google_Auth_AssertionCredentials(
        GAPI_SERVICE_ACCOUNT, array($service), file_get_contents(GAPI_KEYFILE)
    )
);

if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion();
};

$client->setClientId(GAPI_CLIENT_ID);
$service = Google_Service_Webmasters($client);

AFTER:

$client = new Google_Client();
$client->setApplicationName('API Project');
$client->setDeveloperKey(GAPI_KEY);
$client->setSubject(GAPI_SERVICE_ACCOUNT);
$scopes = [ Google_Service_Webmasters::WEBMASTERS ];
$client->setAuthConfig('client_secret.json');   
$client->setScopes($scopes);

$service = Google_Service_Webmasters($client);

And I get this Exception when I'm doing $service->searchanalytics->query(........):

object(Google_Service_Exception)#53 (8) {
  ["errors":protected]=>
  array(1) {
    [0]=>
    array(5) {
      ["domain"]=>
      string(6) "global"
      ["reason"]=>
      string(8) "required"
      ["message"]=>
      string(14) "Login Required"
      ["locationType"]=>
      string(6) "header"
      ["location"]=>
      string(13) "Authorization"
    }
  }
  ["message":protected]=>
  string(238) "{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

...

I also get these notices:

PHP Notice:  Undefined index: client_secret in google-api-php-client/vendor/google/apiclient/src/Google/Client.php on line 862

Notice: Undefined index: client_secret in google-api-php-client/vendor/google/apiclient/src/Google/Client.php on line 862

Any ideas? Thanks!

来源:https://stackoverflow.com/questions/37942676/setassertioncredentials-on-google-api-php-client-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!