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

gmail api php curl使用oauth2创建过滤器,php,curl,google-api,gmail-api,php-curl,Php,Curl,Google Api,Gmail Api,Php Curl,我尝试将CURL与PHP结合使用,用gmailapi创建过滤器 到目前为止,我成功地创建/删除了新用户,添加和删除了标签,但我阻止了该部分 代码如下: <?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_SEPAR

我尝试将CURL与PHP结合使用,用gmailapi创建过滤器

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

代码如下:

<?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));
?>
access\u令牌;
$headers=数组(
“内容类型:应用程序/atom+xml”,
“内容长度:”.strlen($data),
“授权:”.$key,
“X-HTTP-Method-Override:POST”);
//设置并执行Curl请求:
$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);
打印($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>

未知授权标头
未知授权标头
错误401
是否还有其他方法使工作成为O2Authentification

以下是我允许的范围列表:

电子邮件设置(读/写)https://apps-apis.google.com/a/feeds/emailsettings/2.0/ 
电子邮件(读/写/发送)https://mail.google.com/ 
查看和管理域上组的设置https://www.googleapis.com/auth/admin.directory.group 
查看和管理域上的用户资源调配https://www.googleapis.com/auth/admin.directory.user 
电子邮件(管理标签)https://www.googleapis.com/auth/gmail.labels 
电子邮件(读/写)https://www.googleapis.com/auth/gmail.modify


提前感谢您。

每个对Gmail API的请求都需要一个访问令牌,所以API密钥是不够的。虽然用户可能已经登录,但其本身并没有授权您的应用程序访问用户的Gmail

与其他GoogleRESTAPI一样,Gmail API用于处理身份验证和授权。尽管您可以显式地编写web服务身份验证调用的代码,但您通常应该使用适用于多种编程语言的Google API客户端库来简化应用程序

有关在Gmail API中使用auth的更多信息,请参阅