令牌中的细分数量错误(OAuth Google Api) [英] Wrong number of segments in token (OAuth Google Api)

查看:64
本文介绍了令牌中的细分数量错误(OAuth Google Api)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最终目标是通过Google Gmail API向自己发送电子邮件.

My end goal is to send email to myself via Google Gmail API.

这是我的问题.

当我获取访问令牌时,会弹出一个错误

When i'm getting my access token an error pops up

 Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Wrong number of segments in token: '

我在此处阅读云端点oauth2错误,表示"这并不意味着您令牌无效".,但是我收到一个致命错误,中断了我的脚本.

I read here Cloud endpoints oauth2 error that "This DOES NOT mean your token was invalid", but i'm getting a fatal error that interrupting my script.

我的访问令牌看起来像这样 4/MqiIIl5K4S3D4iiieHshQt5D4M79oo07SbhMn22oe2o.cswa8t9ZuDAfJvIeHux6iLYXpNQmlAI

My access token looks like this 4/MqiIIl5K4S3D4iiieHshQt5D4M79oo07SbhMn22oe2o.cswa8t9ZuDAfJvIeHux6iLYXpNQmlAI

如果我使用此令牌刷新页面,则会收到另一个错误,即

If I refresh the page with this token i'd get another error, which is

'Error fetching OAuth2 access token, message: 'invalid_grant: Invalid code.'

这是我的代码

<?php

include_once "templates/base.php";
echo pageHeader("Simple API Access");

require_once realpath(dirname(__FILE__) . '/../autoload.php');


$client = new Google_Client();


$client_id = '114600397795-j5sn0gvsdrup0s8dcmsj49iojp3m9biu.apps.googleusercontent.com';
$client_secret = 'm3Dzankql_rs1OGICsA3Hbtc';
$redirect_uri = 'http://alupa.com/gmail/examples/simple-query.php';

$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);

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

$apiKey = "AIzaSyCWXxrTshKsotxEYNZZCXxdVXhLeku55cw"; 
$client->setDeveloperKey($apiKey);


if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  //$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  //header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}else{

$client->setApplicationName("Client_Gmail_Examples");

}


if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $authUrl = $client->createAuthUrl();
}

if ($client->getAccessToken()) {
  $_SESSION['access_token'] = $client->getAccessToken();
  $token_data = $client->verifyIdToken()->getAttributes();
}


?>
<div class="box">
  <div class="request">
<?php
if (isset($authUrl)) {
  echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
} else {
  echo "<a class='logout' href='?logout'>Logout</a>";
}
?>
  </div>
</div>

alupa.com是我的本地域,我看不到任何问题

alupa.com is my local domain and I don't see any problems with that

我正在使用Google的原始库 https://github.com/google/google-api-php-client

I'm using original library from google https://github.com/google/google-api-php-client

推荐答案

更改:

$token_data = $client->verifyIdToken()->getAttributes();

收件人:

$tuan = $client->getAccessToken();
$token_data = $client->verifyIdToken($tuan->id_token);

这篇关于令牌中的细分数量错误(OAuth Google Api)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆