Google Drive SDK:从generateAssertion()获取令牌后的下一步



好吧,我正在尝试使用Google Drive SDK设置我的应用程序。我把它用作服务帐户,所以设置有点不同。这就是我做事的方式:

包括所需文件:

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/auth/Google_AssertionCredentials.php';
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";

定义常量:

DEFINE("TRUE_PATH",$_SERVER["DOCUMENT_ROOT"].'path/to/file');
DEFINE("SERVICE_ACCOUNT_EMAIL","somecode@developer.gserviceaccount.com");
DEFINE("DRIVE_SCOPE","https://www.googleapis.com/auth/drive");

功能:

function generateAssertion() {
$assertionCredentials = new Google_AssertionCredentials();
$assertionCredentials->serviceAccountName='somecode@developer.gserviceaccount.com';
$assertionCredentials->privateKey=
TRUE_PATH.'9e01fd1414aa082fadeec316161eb7028558fbde-privatekey.p12';
$assertionCredentials->privateKeyPassword = 'notasecret';
$p12cert = array();
$file = TRUE_PATH.'9e01fd1414aa082fadeec316161eb7028558fbde-privatekey.p12';
if (file_exists($file)) {
try {
$assertion = $assertionCredentials->generateAssertion();
return $assertion;
} catch (Exception $e) {
d($assertionCredentials);
return 'Caught exception: ' . $e->getMessage() . "n";
}
} else {
return "no p12 privatekey file";
}
}

行$assertion=$assertitionCredentials->generateAssertion();给了我一个代码,看起来像这样:

eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJodHRwczpcL1wvYWNjb3VudHMuZ29vZ2xlLmNvbVwvb1wvb2F1dGgyXC90b2tlbiIsInNjb3blIjpudWxsLCJpYxQiOjEzNTMzNjk3nzUsImV4cCI6MTM1MzM3MzM3NSwiaXNzIjoiMjY5Mjg1NDU2NjY0QgrldmVsb3Brci5nc2VydmljZWFjY291bnQuY29 tIn0.KMBTsGZkhLhddBFtc0PB1qZgtdQYyirhWtYixCYx1zo5Otv9pCUBfvrCXOg3JzR-mzE6zHuCZdmGOD7w_LRwYHJpoo0rdpDXLjtNCgFZdu1d21cVAnqTkIhMGvdS_K7JP_KioXyi-nBdvZt9IXKaApIdDKhRp-T5HByIeO2ijU

我的问题是,如何处理generateAssertion()返回的代码/令牌,以便访问我的驱动器帐户?我尝试将它用作访问令牌和刷新令牌,但没有成功。

如果这是一个愚蠢的问题,我很抱歉,但我是sdk的新手任何帮助都将不胜感激。

谢谢!!!

您不需要调用generateAssertion()。相反,只需将凭据提供给客户端实例,让它完成生成断言的工作

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

请参阅http://code.google.com/p/google-api-php-client/wiki/OAuth2了解更多详细信息。

最新更新