I want to create label in gmail using google-api but i get error Token invalid - AuthSub token has wrong scope -
my client_id, secret_id, developer_key , redirect_uri auth request set in config file included. use following code create label in gmail account using google-api-php-client :-
<?php error_reporting(e_all); ini_set('display_errors', 1); // set include path google php client library set_include_path( get_include_path() . path_separator . 'google-api-php-client-master/src' ); require_once 'google-api-php-client-master/src/google/client.php'; $client = new google_client(); // call when redirect after callback google oauth if (isset($_get['code'])) { $client->authenticate($_get['code']); $_session['access_token'] = $client->getaccesstoken(); if (isset($_session['access_token']) && $_session['access_token']) { $client->setaccesstoken($_session['access_token']); } $data = '<?xml version="1.0" encoding="utf-8"?> <atom:entry xmlns:atom="http://www.w3.org/2005/atom" xmlns:apps="http://schemas.google.com/apps/2006"> <apps:property name="label" value="testcreatedbyapi" /> </atom:entry>'; $headers = array( "x-http-method-override: post", "authorization: bearer $access_token", "content-type: application/atom+xml" ); $ch = curl_init(); curl_setopt($ch, curlopt_url, "https://apps-apis.google.com/a/feeds/emailsettings/2.0/google.com/{username}/label"); curl_setopt($ch, curlopt_httpheader, $headers); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_postfields, $data); print_r($response = curl_exec($ch)); } $client->addscope("https://www.googleapis.com/auth/userinfo.email"); $client->addscope("https://mail.google.com"); $authurl = $client->createauthurl(); ?>
this view section of code:
<div class="box"> <div class="request"> <?php if (isset($authurl)): ?> <a class='login' href='<?php echo $authurl; ?>'>connect me!</a> <?php else: ?> <form id="url" method="get" action="<?php echo $_server['php_self']; ?>"> <input name="url" class="url" type="text"> <input type="submit" value="shorten"> </form> <a class='logout' href='?logout'>logout</a> <?php endif ?> </div> <?php if (isset($short)): ?> <div class="shortened"> <?php var_dump($short); ?> </div> <?php endif ?> </div>
i use add scope create label:
$client->addscope("https://www.googleapis.com/auth/userinfo.email"); $client->addscope("https://mail.google.com");
i following error:
token invalid - authsub token has wrong scope error 401
the right scope creating email label
https://apps-apis.google.com/a/feeds/emailsettings/2.0/
check out documentation here: https://developers.google.com/admin-sdk/email-settings/auth
and here:
https://developers.google.com/admin-sdk/email-settings/#manage_labels
Comments
Post a Comment