无法使用Google Calendar Api v3获取访问令牌 [英] Cannot get access token with Google Calendar Api v3

查看:71
本文介绍了无法使用Google Calendar Api v3获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将日历事件放入没有身份验证提示的日历中.我已经读过,使用OAuth服务帐户可以做到这一点.我已经在我的Google Developer Console中设置了服务帐户,对此没有任何运气.

I am trying to put a calendar event into a calendar without the authentication prompt. I have read that using an OAuth Service Account allows you to do this. I have set up the Service account in my Google Developer Console and am not having any luck with this.

我们有一个Google企业帐户,并且我将委派的用户设置为我们帐户的管理员,以便他们可以访问任何人的日历,但不会创建活动.我认为问题出在访问令牌上.当我var_dump($accessToken)时显示NULL.

We have a Google business account and I am setting my delegated user to an admin of our account so they can access anyone's calendar but it will not create the event. I believe the issue is with the access token. When I var_dump($accessToken) it shows NULL.

<?php
error_reporting(E_ALL);
require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';
session_start();    

/* Authentication ID's */    
const CLIENT_ID = '[MY CLIENT ID]';
const SERVICE_ACCOUNT_NAME = '[MY SERVICE ACCOUNT]';
const KEY_FILE = '[MY KEY FILE].p12';
const CALENDAR_SCOPE = "https://www.googleapis.com/auth/calendar";
$key = file_get_contents(KEY_FILE);
$auth = new Google_Auth_AssertionCredentials(
  SERVICE_ACCOUNT_NAME,
  array(CALENDAR_SCOPE),
  $key
);
$auth->sub = "adminaccount@email.com";

$client = new Google_Client();
$client->setScopes(array(CALENDAR_SCOPE));
$client->setAssertionCredentials($auth);

$client->getAuth()->refreshTokenWithAssertion();
$accessToken = $client->getAccessToken();

$client->setClientId(CLIENT_ID);

$user = "testaccount@email.com";

if($accessToken){

  $cal = new Google_Service_Calendar($client);

  $event = new Google_Service_Calendar_Event();
  $event->setSummary('TITLE');
  $event->setLocation('World');
  $event->setDescription('test');

  $start = new Google_Service_Calendar_EventDateTime();
  $start->setDate(date('yyyy-mm-dd'));
  $event->setStart($start);

  $end = new Google_Service_Calendar_EventDateTime();
  $end->setDate(date('yyyy-mm-dd'));
  $event->setEnd($end);

  $cal->events->insert($user, $event);
}
?>

我已经引用了该其他线程(使用服务帐户getAccessToken ()返回null ),尝试解决此问题,但运气不佳.

I have referenced this other thread (Using a Service Account, getAccessToken() is returning null) to try and troubleshoot this issue but have had no luck with it.

任何帮助将不胜感激!

推荐答案

听起来您需要授予对Google Apps域用户数据的访问权限.链接下面的将域范围的权限委派给您的服务帐户"部分.

It sounds like you need to grant access to the google apps domain user data. Link below under section "Delegate domain-wide authority to your service account".

https://developers.google.com/drive/web/delegation

这篇关于无法使用Google Calendar Api v3获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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