Php 我从哪里获得授权的Gmail API服务实例?
我正在尝试使用Gmail API和php更新标签 我已经复习了这个问题,但它是关于python的,对我来说没有多大意义 我指的是代码 具体而言,这:Php 我从哪里获得授权的Gmail API服务实例?,php,gmail,gmail-api,Php,Gmail,Gmail Api,我正在尝试使用Gmail API和php更新标签 我已经复习了这个问题,但它是关于python的,对我来说没有多大意义 我指的是代码 具体而言,这: /** * Update an existing Label. * * @param Google_Service_Gmail $service Authorized Gmail API instance. * @param string $userId User's email address. The special val
/**
* Update an existing Label.
*
* @param Google_Service_Gmail $service Authorized Gmail API instance.
* @param string $userId User's email address. The special value 'me'
* can be used to indicate the authenticated user.
* @param string $labelId ID of Label to update.
* @param string $labelName Updated name.
* @param string $labelListVisibility Updated Label list visibility.
* @param string $messageListVisibility Updated Message list visibility.
* @return Google_Service_Gmail_Label updated Label.
*/
function updateLabel($service, $userId, $labelId, $labelName,
$labelListVisibility, $messageListVisibility) {
$label = new Google_Service_Gmail_Label();
$label->setName($labelName);
$label->setLabelListVisibility($labelListVisibility);
$label->setMessageListVisibility($messageListVisibility);
try {
$label = $service->users_labels->update($userId, $labelId, $label);
print 'Label with ID: ' . $labelId . ' successfully updated.';
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
return $label;
}
我想我已经计算出了所有的参数,但是它说
* @param Google_Service_Gmail $service Authorized Gmail API instance.
我不知道“授权Gmail API实例”指的是什么
我用API连接并验证了身份(我可以列出标签),所以我猜它在寻找我的连接的一些参考。但我不知道在哪里找到这个,也不知道如何表达它
以下代码用于列出标签(gauth.php):
和settings.php:
/* Google App Client Id */
define('CLIENT_ID', 'xxxxxxxxxxxxxxxx.apps.googleusercontent.com');
/* Google App Client Secret */
define('CLIENT_SECRET', 'xxxxxxxxxxxxxxxxxxx');
/* Google App Redirect Url */
define('CLIENT_REDIRECT_URL', 'http://localhost:8888/xxxxxxxx/gauth/gauth.php');
这也把我绊倒了!谷歌最近似乎并不关心PHP,是吗?他们的文档真的和人们说的一样糟糕
$gmail = new Google_Service_Gmail($client);
这就是我为克服这一障碍所做的。我认为他们的意思是,你必须创建一个像上面那样的特定服务对象,而不是仅仅使用客户机对象本身?它可以用更好的措辞,也可以扩展
祝你好运!(您需要它!)要每次都获取访问令牌,只需使用有效凭据调用此函数即可
function GetRefreshedAccessToken($client_id, $refresh_token, $client_secret) {
$url_token = 'https://www.googleapis.com/oauth2/v4/token';
$curlPost = 'client_id=' . $client_id . '&client_secret=' . $client_secret . '&refresh_token='. $refresh_token . '&grant_type=refresh_token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = json_decode(curl_exec($ch), true); //print_r($data);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception('Error : Failed to refresh access token');
return $data;
}
$gmail = new Google_Service_Gmail($client);
function GetRefreshedAccessToken($client_id, $refresh_token, $client_secret) {
$url_token = 'https://www.googleapis.com/oauth2/v4/token';
$curlPost = 'client_id=' . $client_id . '&client_secret=' . $client_secret . '&refresh_token='. $refresh_token . '&grant_type=refresh_token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = json_decode(curl_exec($ch), true); //print_r($data);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception('Error : Failed to refresh access token');
return $data;
}