@note涉及的服务器流程使用google-api-php-client-beta
目标是将“电子邮件”邮件“插入”发送到已注册电子邮件别名的Google for Work Gmail帐户。
对于此OAuth工作流程,我有:
以下服务器代码尝试获取使用上述凭据和访问令牌授权的Google_Client,同时要求OAuth客户端模拟提及的GFW用户。此后,$ client可用于调用Google_Service_Gmail等服务。
目前,$authCreds
将返回 ['error_description'] => “无法确定来自请求的客户端ID 错误消息
// local const's for testing
const CLNT_GOOGL_SRVC_ACCT_CREDS = '';
const CLNT_GOOGL_API_SRVC_ACCT_CLIENT_ACCESS_CODE = '';
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
private static function getAuthorizedClient() {
$client = new Google_Client();
$isSrvcAcct = false;
if (self::CLNT_GOOGL_SRVC_ACCT_CREDS) {
$isSrvcAcct = true;
$creds = json_decode(self::CLNT_GOOGL_SRVC_ACCT_CREDS, true);
} elseif (defined('CLNT_GOOGL_SRVC_ACCT_CREDS')) {
$isSrvcAcct = true;
$creds = json_decode(CLNT_GOOGL_SRVC_ACCT_CREDS, true);
}
if ($isSrvcAcct) {
if (! isset($creds)) {
acapApp::catchProcessMessage('CLNT_GOOGL_SRVC_ACCT_CREDS is not valid JSON?', ACAP_DEBUG);
return;
}
self::setServiceClient($client, (array) $creds);
} else {
self::setUserClient($client);
}
if ($client->getAccessToken()) {
return $client;
}
}
private static function setServiceClient(Google_Client $client, array $creds) {
$client->setAuthConfig($creds);
$client->setSubject(CLNT_SMTP_OAUTH_USER_EMAIL);
if (self::CLNT_GOOGL_API_SRVC_ACCT_CLIENT_ACCESS_CODE) {
$authCode = self::CLNT_GOOGL_API_SRVC_ACCT_CLIENT_ACCESS_CODE;
} else if (defined('CLNT_GOOGL_API_SRVC_ACCT_CLIENT_ACCESS_CODE')) {
$authCode = CLNT_GOOGL_API_SRVC_ACCT_CLIENT_ACCESS_CODE;
}
if (isset($authCode)) {
$authCreds = $client->fetchAccessTokenWithAuthCode($authCode);
if (isset($authCreds['error'])) {
acapApp::catchProcessMessage('Failure Fetching Auth Token: '.$authCreds['error_description'], ACAP_DEBUG);
}
}
}
答案 0 :(得分:1)
如果你使用.json文件,你必须使用它来进行身份验证: $客户端 - > fetchAccessTokenWithAssertion()
答案 1 :(得分:1)
确保您在之前设置授权配置以使用身份验证代码获取访问令牌。
例如:
<?php
$client->setAuthConfig(storage_path('app/google_client_id.json'));
$token = $client->fetchAccessTokenWithAuthCode($code);