【发布时间】:2014-01-16 18:22:06
【问题描述】:
我正在尝试对服务帐户进行身份验证,以便可以将访问令牌与客户端 JSON_API 库一起使用。
我看过这些文章:
https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php
https://code.google.com/p/google-api-php-client/wiki/UsingTheLibrary
https://developers.google.com/storage/docs/authentication#service_accounts https://developers.google.com/accounts/docs/OAuth2#scenarios
这是我的 PHP 代码
<?php
require_once 'google-api-php-client/src/Google_Client.php';
const CLIENT_ID = "";
const SERVICE_ACCOUNT_NAME = "";
const KEY_FILE = "super secret path of course ;)";
$client = new Google_Client();
// Loads the key into PKCS 12 format
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/prediction'),
$key
)
);
$client->setClientId(CLIENT_ID);
$auth = $client->authenticate();
print $auth ? "Returned true" : "Returned false";
print "<br>";
print is_null($client->getAccessToken()) ? "It's null" : "Works";
?>
这是我的输出:
返回真
是空的
【问题讨论】:
标签: php google-oauth google-cloud-storage google-authentication