gmail api php curl使用oauth2创建过滤器

时间:2016-04-03 04:11:12

标签: php curl google-api gmail-api php-curl

我尝试使用CURL与PHP一起使用GMAIL API创建过滤器。

到目前为止,我设法创建/删除新用户,添加和删除标签,但我阻止了该部分。

以下是代码:

<?php
//load the necessary

require_once '/root/gmail/google-api-php-client/src/Google/autoload.php';
set_include_path("/root/gmail/google-api-php-client/src" . PATH_SEPARATOR . "/root/gmail/google-api-php-client/examples" . PATH_SEPARATOR. get_include_path()); 

include_once "templates/base.php"; 
require_once 'Google/Client.php'; 
require_once 'Google/Service/Directory.php'; 
require_once 'Google/Service/Gmail.php';

//PARAM IDENTIFICATION AND SCOPE
$client_id = '<CLIENT_ID>.apps.googleusercontent.com';
$service_account_name = '<SERVICE>.iam.gserviceaccount.com';
$key_file_location = '<PATH_TO_CERT>.p12';
$scope = array("https://apps-apis.google.com/a/feeds/emailsettings/2.0/", "https://mail.google.com/");
$service_token = null;

// Start auth process: 
$client = new Google_Client();    
$client->setApplicationName("managegmail");

// Create the service
$service = new Google_Service_Gmail($client);
if (isset($service_token)) 
{
    $client->setAccessToken($service_token);
}

// SET Credential
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
$scope,
$key,
'notasecret',
'http://oauth.net/grant_type/jwt/1.0/bearer',
'<account>@<my_domain.com>'
);

// IF Auth OK then OK.
$client2->setAssertionCredentials($cred2);
if($client2->getAuth()->isAccessTokenExpired()) 
{
   $client2->getAuth()->refreshTokenWithAssertion($cred2);
}

// prepare the data packet for curl :
$data  = '<?xml version="1.0" encoding="utf-8"?>';
$data .= "<atom:entry xmlns:atom='http://www.w3.org/2005/Atom' 
xmlns:apps='http://schemas.google.com/apps/2006'>";
$data .= "<apps:property name='from' value='<toexclude@domain.com>' />";
$data .= "<apps:property name='label' value='<label_name>' />";
$data .= "<apps:property name='neverSpam' value='true' />";
$data .= "</atom:entry>";

//Set the Header with the Token :
$key = $jsonservice_token->access_token;
$headers = array(
"Content-type: application/atom+xml",
"Content-length: " . strlen($data),
"Authorization: "  . $key,
"X-HTTP-Method-Override: POST");

// Set and Execute Curl request :
$ch      = curl_init();
curl_setopt($ch, CURLOPT_URL,            "https://apps-apis.google.com/a/feeds/emailsettings/2.0/unity3d.com/test1234567/filter");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,     $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS,     $data);
print_r($response = curl_exec($ch));
?>

每次收到以下错误:

<HTML>
<HEAD>
<TITLE>Unknown authorization header</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unknown authorization header</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

还有别的办法让O2auth认证工作吗?

这里也是我允许的范围列表:

Email Settings (Read/Write) https://apps-apis.google.com/a/feeds/emailsettings/2.0/ Email (Read/Write/Send) https://mail.google.com/ View and manage the provisioning of groups on your domain https://www.googleapis.com/auth/admin.directory.group View and manage the provisioning of users on your domain https://www.googleapis.com/auth/admin.directory.user Email (Manage labels) https://www.googleapis.com/auth/gmail.labels Email (Read/Write) https://www.googleapis.com/auth/gmail.modify

先谢谢你。

1 个答案:

答案 0 :(得分:0)

每个Gmail API请求都需要一个访问令牌,因此APi密钥是不够的。虽然用户可能已登录但本身并未授权您的应用授权访问用户的Gmail。

与其他Google REST API一样,Gmail API使用OAuth 2.0来处理身份验证和授权。虽然您可以明确地对Web服务身份验证调用进行编码,但通常应该使用适用于许多编程语言的Google API客户端库来简化您的应用程序。

有关在Gmail API中使用auth的详情,请参阅Authorizing Your App with Gmail