简体   繁体   中英

gmail api php curl create filters with oauth2

I try to use CURL with PHP to create filters with 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 ?

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. While the user may have logged in that does not by itself give your app authorized access to the user's Gmail.

Like other Google REST APIs, the Gmail API uses OAuth 2.0 to handle authentication and authorization. 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.

For more about using auth with the Gmail API, see Authorizing Your App with Gmail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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