2012-06-14 73 views
1

我正在为存储API创建API请求(GET存储桶),并且其中一个必需参数是“授权”标头。使用服务帐户为Google Cloud Storage REST API请求OAuth2访问令牌时发生invalid_grant错误

请注意,我用的是服务帐户访问API。

我跟着文档https://developers.google.com/accounts/docs/OAuth2ServiceAccount获得的“授权”标头的访问令牌,这样我就可以发送授权的请求给他们的REST API。问题是我总是得到“invalid_grant”错误。

使用此代码检查出来:

<?php 
error_reporting(E_ERROR); 

const CLIENT_ID = 'XXXXXXXXXXXX.apps.googleusercontent.com'; 
const SERVICE_ACCOUNT = '[email protected]'; 
const KEY_FILE = 'XXX.p12'; 

function get_oauth_access_token() 
{ 
    $header[alg] = 'RS256'; 
    $header[typ] = 'JWT'; 

    $header = urlencode(base64_encode(utf8_encode(json_encode($header)))); 

    $assertion_time = time(); 

    $claim[iss] = CLIENT_ID; //also tried SERVICE_ACCOUNT here, no improvement 
    $claim[scope] = 'https://www.googleapis.com/auth/devstorage.read_only'; 
    $claim[aud] = 'https://accounts.google.com/o/oauth2/token'; 
    $claim[exp] = $assertion_time + 3600; 
    $claim[iat] = $assertion_time; 

    $claim = urlencode(base64_encode(utf8_encode(json_encode($claim)))); 

    $data = $header . '.' . $claim; 

    $p12 = file_get_contents(KEY_FILE); 
    $cert = array(); 
    openssl_pkcs12_read($p12, $cert, 'notasecret'); 
    $priv_key_id = openssl_get_privatekey($cert[pkey]); 

    openssl_sign($data, $signature, $priv_key_id, 'sha256'); 

    $signature = urlencode(base64_encode($signature)); 

    $assertion = $data . '.' . $signature; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token'); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('grant_type'=>'assertion', 
               'assertion_type'=>'http://oauth.net/grant_type/jwt/1.0/bearer', 
               'assertion'=>$assertion)); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 


    $result = curl_exec($ch); 
    $error = curl_error($ch); 

    curl_close($ch); 
    var_dump($result); 
    var_dump($error); 
} 

get_oauth_access_token(); 

有什么不对这段代码中导致“invalid_grant”的错误?

回答

0

这里的说明使用与谷歌云存储的RESTful HTTP接口的服务帐户的一个简单的PHP程序。我用一个服务帐户测试了这个代码,它似乎工作正常。如果您还有其他问题,请告诉我。

<?php 

require_once 'apiClient.php'; 

// Define constants. 
const CLIENT_ID = 'YOUR_CLIENT_ID_GOES_HERE'; 
const SERVICE_ACCOUNT_NAME = 'YOUR_SERVICE_ACCOUNT_NAME_GOES_HERE'; 
const KEY_FILE = 'key.p12'; 
const BUCKET = 'marc-us'; 

// Obtain OAuth 2.0 access token for service account. 
$client = new apiClient(); 
$key = file_get_contents(KEY_FILE); 
$client->setAssertionCredentials(new apiAssertionCredentials(
    SERVICE_ACCOUNT_NAME, 
    array('https://www.googleapis.com/auth/devstorage.full_control'), 
    $key) 
); 
$client::$auth->refreshTokenWithAssertion(); 
$json = $client->getAccessToken(); 
$accessToken = json_decode($json)->access_token; 

// Add access token to Authorization header of HTTP request. 
$ch = curl_init('http://commondatastorage.googleapis.com/' . BUCKET); 
curl_setopt($ch, CURLOPT_HTTPHEADER, 
      array('Authorization: OAuth ' . $accessToken)); 
$resp = curl_exec($ch); 
curl_close($ch); 

// Display results. 
print '<h2>Response:</h2><pre>' . $resp . '</pre>'; 
?> 
+0

不,这是行不通的,因为我已经在这里说: https://groups.google.com/forum/?fromgroups#!topic/gs-discussion/3y_2XVE2q7U 你建议的代码可能只是一个解决方法,并增加了膨胀,这需要SDK才能获得访问令牌。 –

0

在我最后的响应代码工作正常,我所以我怀疑你面对的环境问题。无论如何,我咨询了Google PHP客户端库的所有者,他提供了一种刷新访问令牌的更好方法,而无需调用内部的refreshTokenWithAssertion()方法。他建议这种技术:根据需要

$req = new apiHttpRequest(YOUR_URL); 
    $val = $client->getIo()->authenticatedRequest($req); 

到authenticatedRequest()的调用需要(如果他们设定断言凭证)刷新访问令牌的照顾。我修改了上面的代码来使用这种方法,它对我来说工作正常。请注意,旧版本和新版本都适用于我,因此没有功能差异,但我认为新版本更好,因为它避免了内部调用,使用Google PHP客户端库代替curl执行请求,并且结果更短,更简单的代码。

<?php 

require_once 'apiClient.php'; 

// Define constants. 
const SERVICE_ACCOUNT_NAME = 'YOUR_SERVICE_ACCOUNT_NAME'; 
const KEY_FILE = 'key.p12'; 
const BUCKET = 'marc-us'; 

// Obtain service account credentials assertion. 
$client = new apiClient(); 
$key = file_get_contents(KEY_FILE); 
$client->setAssertionCredentials(new apiAssertionCredentials(
    SERVICE_ACCOUNT_NAME, 
    array('https://www.googleapis.com/auth/devstorage.full_control'), 
    $key) 
); 

// Create HTTP request, authorize and execute. 
$req = new apiHttpRequest('http://commondatastorage.googleapis.com/' . BUCKET); 
$resp = $client::getIo()->authenticatedRequest($req); 

// Display results. 
print '<h2>Response:</h2>' . $resp->getResponseBody(); 

?> 
2

您的问题可能是您的服务器的时间。

我面临着谷歌分析一个问题:我的代码是正确的,但与谷歌的时间比我的服务器的时间有几秒钟的未来。您可以将服务器的时间延迟几分钟来进行测试。

如果一切正常,你可以使用NTP例如,保持服务器时钟正确。

0

我有同样的错误,但与Java。我的问题是exp和iat在几秒钟内的时间是在格林威治标准时间-5,而谷歌谷歌认证是在格林尼治标准时间。然后我将时间更改为GMT值,一切正常。

相关问题