2016-04-03 42 views
0

我嘗試使用CURL與PHP創建使用GMAIL API的過濾器。gmail api php curl使用oauth2創建過濾器

到目前爲止,我設法創建/刪除新用戶,添加和刪除標籤,但我阻止了該部分。

下面是代碼:

<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認證:

<?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='<[email protected]>' />"; 
$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)); 
?> 

每次我得到以下錯誤的時間?

這裏也是我允許的範圍列表:

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

通過提前謝謝。

回答

0

對Gmail API的每個請求都需要一個訪問令牌,所以APi密鑰不夠用。雖然用戶可能已登錄,但本身並不能讓您的應用授權訪問用戶的Gmail。

與其他Google REST API一樣,Gmail API使用OAuth 2.0來處理認證和授權。雖然您可以顯式編碼Web服務身份驗證調用,但您通常應該使用適用於許多編程語言的Google API客戶端庫來簡化應用程序。

有關通過Gmail API使用身份驗證的更多信息,請參見Authorizing Your App with Gmail