all*_*arn 6 php google-api oauth-2.0 google-oauth google-api-php-client
我正在运行以下PHP代码,使用此处的客户端库:https://code.google.com/p/google-api-php-client/.我没有得到任何这个代码的错误,但是当我调用时getAccessToken(),它返回null.
我允许在个人日历上访问此服务帐户,并通过API控制台授予对项目的完全访问权限.
有任何想法吗?
require_once 'google-api-php-client/src/Google_Client.php';
const CLIENT_ID = 'blahblahblah';
const SERVICE_ACCOUNT_NAME = 'blahblahblah@developer.gserviceaccount.com';
const KEY_FILE = 'path/to/privatekey.p12';
$google_client = new Google_Client(); // created only to initialized static dependencies
$client = new Google_OAuth2(); // you really just need Google_OAuth2
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(
new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key
)
);
var_dump($client->getAccessToken());
Run Code Online (Sandbox Code Playgroud)
出于某种原因,这似乎有效:
require_once 'google-api-php-client/src/Google_Client.php';
const CLIENT_ID = 'blahblahblah';
const SERVICE_ACCOUNT_NAME = 'blahblahblah@developer.gserviceaccount.com';
const KEY_FILE = 'path/to/privatekey.p12';
const CALENDAR_SCOPE = "https://www.googleapis.com/auth/calendar";
$key = file_get_contents(KEY_FILE);
$auth = new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array(CALENDAR_SCOPE),
$key
);
$client = new Google_Client();
$client->setScopes(array(CALENDAR_SCOPE));
$client->setAssertionCredentials($auth);
$client->getAuth()->refreshTokenWithAssertion();
$accessToken = $client->getAccessToken();
$client->setClientId(CLIENT_ID);
Run Code Online (Sandbox Code Playgroud)
如果有人可以解释为什么这有效,请编辑此答案或评论!
| 归档时间: |
|
| 查看次数: |
6013 次 |
| 最近记录: |