【发布时间】:2018-04-04 18:38:15
【问题描述】:
我正在尝试使用 Gmail API 和 php 更新标签。
我已经查看了here 的问题,但它是关于 python 的,对我来说没有多大意义。
我指的是代码here。
具体来说,这个:
/**
* 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):
<?php
session_start();
require_once('settings.php');
require_once('google-login-api.php');
// Google passes a parameter 'code' in the Redirect Url
if(isset($_GET['code'])) {
try {
$gapi = new GoogleLoginApi();
// Get the access token
$data = $gapi->GetAccessToken(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $_GET['code']);
// Get user information
$user_labels = $gapi->ListUserLabels($data['access_token']);
$labels_array = $user_labels["labels"];
$i = 0;
while($i <= count($labels_array))
{
if($labels_array[$i]['type'] == "user")
{
echo $labels_array[$i]['name']." - ".$labels_array[$i]['id'];
echo'<br />';
}
$i++;
}
// Now that the user is logged in you may want to start some session variables
$_SESSION['logged_in'] = 1;
// You may now want to redirect the user to the home page of your website
// header('Location: home.php');
}
catch(Exception $e) {
echo $e->getMessage();
exit();
}
}
这里是 google-login-api.php:
class GoogleLoginApi
{
public function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) {
$url = 'https://accounts.google.com/o/oauth2/token';
$curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
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);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception('Error : Failed to receieve access token');
return $data;
}
public function ListUserLabels($access_token) {
//$url = 'https://www.googleapis.com/plus/v1/people/me';
$url = 'https://www.googleapis.com/gmail/v1/users/me/labels';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception('Error : Failed to get user information');
return $data;
}
}
还有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');
【问题讨论】: