简体   繁体   English

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

[英]gmail api php curl create filters with oauth2

I try to use CURL with PHP to create filters with GMAIL API. 我尝试将CURL与PHP结合使用,以使用GMAIL API创建过滤器。

So far I managed to create/delete new users, add and delete labels but I block for that part. 到目前为止,我设法创建/删除了新用户,添加和删除了标签,但是我阻止了该部分。

Here is the code : 这是代码:

<?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));
?>

every time I get the following error : 每当我得到以下错误:

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

Is there something else to do to make work the O2auth authentification ? 要使O2auth身份验证工作还有其他事情要做吗?

Here is also the list of the scope that I allowed : 这也是我允许的范围列表:

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

Thank you by advance. 预先谢谢你。

Each request to Gmail API requires an access token, so an APi key will not be enough. 对Gmail API的每个请求都需要访问令牌,因此APi密钥是不够的。 While the user may have logged in that does not by itself give your app authorized access to the user's Gmail. 尽管用户可能已经登录,但它本身并未授予您的应用对用户Gmail的授权访问权限。

Like other Google REST APIs, the Gmail API uses OAuth 2.0 to handle authentication and authorization. 与其他Google REST API一样,Gmail API使用OAuth 2.0来处理身份验证和授权。 Although you can code the web service authentication calls explicitly, you normally should simplify your app by using the Google API client libraries available for many programming languages. 尽管您可以显式地编写Web服务身份验证调用的代码,但通常应使用可用于多种编程语言的Google API客户端库来简化您的应用程序。

For more about using auth with the Gmail API, see Authorizing Your App with Gmail 有关将身份验证与Gmail API结合使用的更多信息,请参阅使用Gmail授权应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM