我开始在我的新应用程序上配置谷歌日历.我几乎完成了Google开发人员显示的身份验证代码的完整副本(https://developers.google.com/google-apps/calendar/instantiate),但我不断收到以下错误:
获取OAuth2访问令牌时出错,消息:'invalid_grant'
我目前正在使用Fork-CMS(http://www.fork-cms.com),一个年轻的lightweigth CMS.我正确配置了google-api-php-client的config.php文件.(客户端ID,客户端密码,redirect-uri,开发密钥,...)和重定向uri在google api的控制台上正确设置.我的代码如下:
<?php
/**
* This is a widget with a calendar implementation.
*
* @package frontend
* @subpackage events
*
* @author Michiel Vlaminck <michielvlaminck@gmail.com>
*/
class FrontendEventsWidgetCalendar extends FrontendBaseWidget
{
private $events = array();
private $authUrl = array();
/**
* Execute the extra
*
* @return void
*/
public function execute()
{
// call parent
parent::execute();
// load template
$this->loadTemplate();
// get data
$this->getData();
// parse
$this->parse();
}
/**
* Get …Run Code Online (Sandbox Code Playgroud) 我一直试图从谷歌api使用谷歌客户端"代码"从谷歌客户端返回刷新令牌.它返回我发送到服务器端的代码.现在从服务器端我发送代码以使用google-api-php-client通过此调用获取刷新令牌和访问令牌:
https://www.googleapis.com/oauth2/v4/token
Run Code Online (Sandbox Code Playgroud)
虽然我使用谷歌操场上相同的代码我也得到了刷新令牌的响应,但我没有从我自己的服务器得到它..这是代码
public function getRefreshToken($code)
{
$client = new Google_Client();
$client->setClientId(config('services.google.client_id'));
$client->setClientSecret(config('services.google.client_secret'));
$client->setRedirectUri('postmessage');
$client->setScopes(config('services.google.scopes'));
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
dd($client->authenticate($code));
dd($client->getRefreshToken());
return ;
}
Run Code Online (Sandbox Code Playgroud)
我已经将访问类型设置为离线,如某些答案中所述,但我仍然使用刷新令牌获得响应.这是响应
access_token :"xxxxxxxxxxxxxxxxxxxxxx"
created:1510242052
expires_in:3598
id_token:"xxxxxxxx"
token_type:"Bearer"
Run Code Online (Sandbox Code Playgroud) javascript php google-authentication google-api-php-client google-oauth2
我正在使用Google API客户端来阅读Gmail中的电子邮件.现在我想添加一个Cronjob,每5分钟读一次邮件.
使用Google API客户端的问题是,它需要允许用户首先点击授权链接并允许用户使用Google API客户端.
我有一个类Inbox,其初始化函数初始化了Google API客户端.但是cronjob不起作用,因为我必须获得access_token.
public function initialize() {
$configuration = Configuration::getConfiguration('class_Inbox');
// Creates the Google Client
$this->client = new Google_Client();
$this->client->setApplicationName('Tiptsernetwork');
$this->client->setClientId($configuration['clientId']);
$this->client->setClientSecret($configuration['clientSecret']);
$this->client->setRedirectUri('http://www.tipsternetwork.nl/cronjob/authenticate');
$this->client->addScope('https://mail.google.com/');
$this->client->setApprovalPrompt('force');
$this->client->setAccessType('offline');
// Creates the Google Gmail Service
$this->service = new Google_Service_Gmail($this->client);
// Authenticates the user.
if (isset($_GET['code'])) {
$this->authenticate($_GET['code']);
}
// Check if we have an access token in the session
if (isset($_SESSION['access_token'])) {
$this->client->setAccessToken($_SESSION['access_token']);
} else {
$loginUrl = $this->client->createAuthUrl();
echo '<a href="'.$loginUrl.'">Click Here</a>';
}
// If the token is …Run Code Online (Sandbox Code Playgroud) 我需要创建一个PHP脚本,在Google Calendar上创建一个sigle事件.我在设置客户端ID,客户端密钥,开发密钥和创建新事件时没有任何问题.
我唯一的问题是OAuth2,特别是我需要建立一个永久连接,我不希望每次运行脚本时都进行身份验证.
实际上,使用这个脚本我可以获得一个令牌和一个刷新令牌,但是我的令牌到期时我每小时都知道如何刷新它.如何编辑此代码才能执行此操作?我可以在某处保存令牌和刷新令牌并始终使用相同的数据吗?
我收到了一条未捕获的异常"Google_AuthException",其中显示消息"刷新OAuth2令牌时出错,消息:'{"error":"invalid_grant"
我已经在stackoverflow中阅读了关于此主题的其他一些帖子,但我还没有找到解决方案......:\
<?php
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Calendar App");
$client->setClientId('xxxx');
$client->setClientSecret('yyy');
$client->setRedirectUri('http://www.zzzzzz');
$client->setDeveloperKey('kkk');
$client->setAccessType('offline');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
//echo $_SESSION['token'];
//$client->setAccessToken($_SESSION['token']);
$authObj = json_decode($_SESSION['token']);
$accessToken = $authObj->access_token;
$refreshToken = $authObj->refresh_token;
$tokenType = $authObj->token_type;
$expiresIn = $authObj->expires_in;
echo 'access_token = ' . $accessToken;
echo '<br />';
echo 'refresh_token = ' …Run Code Online (Sandbox Code Playgroud)