谷歌php api身份验证例外

时间:2015-06-09 05:59:08

标签: google-api-php-client

我正在尝试使用Outh2验证Google服务帐户,但一直收到此错误 -

  

异常 - 刷新OAuth2令牌时出错,消息:'{“error”:   “access_denied”,“error_description”:“请求的客户端不是   授权。“}'

我已按照https://developers.google.com/api-client-library/php/auth/service-accounts处的每条指示进行操作 我也从谷歌管理控制台授权服务帐户,但仍然没有运气。任何人都可以建议下面的代码是否有任何错误 -

$client_email = aab123@developer.gserviceaccount.com';
        $private_key = file_get_contents('private_key_file_location.p12');
        $scopes = array('https://spreadsheets.google.com/feeds');
        $user_to_impersonate = 'user@example.com';
        $credentials = new Google_Auth_AssertionCredentials($client_email, $scopes, 
                                                            $private_key, 'notasecret',
                                                           'http://oauth.net/grant_type/jwt/1.0/bearer',
                                                           $user_to_impersonate);

        $client = new Google_Client();

        $client->setApplicationName('Portal Assessment Module');
        $client->setAccessType('offline');

        $client->setAssertionCredentials($credentials);
        if ($client->getAuth()->isAccessTokenExpired()) {
            $client->getAuth()->refreshTokenWithAssertion($credentials);    /* Exception 
is being triggered here */    
}

谢谢。

2 个答案:

答案 0 :(得分:0)

试试这个

require_once realpath(dirname(__FILE__).'/google-api-php-client/src/Google/autoload.php');

define('SCOPES', implode(' ', array(Google_Service_Appsactivity::DRIVE)));

$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    SCOPES,
    $private_key
);

$client = new Google_Client();
$client->setAssertionCredentials($credentials);

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

print_r($client->getAccessToken());

答案 1 :(得分:0)

最后想出来,必须确保$ user_to_impersonate与$ client_email相同,并且该电子邮件地址还具有对电子表格的编辑权限。

var app = angular.module('plunker', ['ngSanitize', 'schemaForm']);
app.controller('MainCtrl', function($scope) {
  $scope.schema = {
    "type": "object",
    "properties": {
      "name": {
        "type": "string",
        "title": "Name",
        "required": true
      },
      "age": {
        "type": "number",
        "title": "Age"
      }
    }

  };

  $scope.form = [
    "*", {
      type: "submit",
      title: "save"
    }
  ];

  $scope.model = {};
});
相关问题