我想使用google-api在gmail中创建标签但我得到错误令牌无效 - AuthSub令牌的范围错误

时间:2014-02-06 09:29:50

标签: google-api google-oauth google-api-php-client

我的client_id,secret_id,developer_key和redirect_uri用于auth请求在包含的配置文件中设置。 我使用google-api-php-client在gmail帐户中使用以下代码创建标签: -

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    // set include path for 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 by 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();

?>

这是代码的视图部分:

<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>

我用于添加范围来创建标签:

 $client->addScope("https://www.googleapis.com/auth/userinfo.email");
 $client->addScope("https://mail.google.com");

我收到以下错误:

Token invalid - AuthSub token has wrong scope
Error 401

1 个答案:

答案 0 :(得分:1)

创建电子邮件标签的正确范围是

https://apps-apis.google.com/a/feeds/emailsettings/2.0/

在此处查看文档:{​​{3}}

也在这里:

https://developers.google.com/admin-sdk/email-settings/auth