错误(401)需要登录 - Google Calendar API v3 [英] Error (401) Login Required - Google Calendar API v3

查看:348
本文介绍了错误(401)需要登录 - Google Calendar API v3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每次在我的codeigniter应用程序中预约约会时,都会将特定活动添加到特定的Google Apps日历。



到目前为止,我已经通过了以下步骤(从将OAuth 2.0用于服务器到服务器应用程序):


  1. 将Google API客户端库添加到应用程序/ third_party中
  2. 使用Google Apps在开发人员控制台中创建服务帐户全域代理启用

  3. 商店提供的.p12文件

  4. 在Google Apps管理控制台中授权服务帐户客户ID,范围 https:/ /www.googleapis.com/auth/calendar

  5. 使用服务帐户电子邮件地址共享日历

我收到错误错误调用POST <日历API插入端点> (401)需要登录



我相信我必须做一些不相关的事情直接在我如何认证。任何建议都会受到欢迎。



application / libraries / Google.php:

 <$ c如果(!defined('BASEPATH'))exit('No direct script access allowed'); $ c><?php 
if;
set_include_path(APPPATH。'third_party /'。PATH_SEPARATOR。get_include_path());
require_once APPPATH。 THIRD_PARTY /谷歌/ autoload.php;
require_once APPPATH。 THIRD_PARTY /谷歌/ Client.php;

class Google扩展Google_Client {
函数__construct($ params = array()){$ b $ parent :: __ construct();

$ client_email ='< SERVICE ACCOUNT EMAIL>';
$ private_key = file_get_contents('< .P12 FILE PATH>');
$ scopes = array('https://www.googleapis.com/auth/calendar');

$ credentials = new Google_Auth_AssertionCredentials(
$ client_email,
$ scopes,
$ private_key
);

$ client = new Google_Client();
$ client-> setAssertionCredentials($ credentials);
if($ client-> getAuth() - > isAccessTokenExpired()){
$ client-> getAuth() - > refreshTokenWithAssertion();



code
$ b

application / controllers / Booking。

(Google.php库在构造函数中加载)

 函数addCalendarEvent($ user_id,$ date,$ time,$ address,$ booking_data){

$ cal = new Google_Service_Calendar($ this-> google);

$ calendar_id ='< CALENDAR ID>';

$ start_datetime = date('c',strtotime($ date。''。$ time));

$ end_datetime = date('c',strtotime($ date。''。$ time。'+2 hours'));

$ event = new Google_Service_Calendar_Event(array(
'summary'=>'Appointment at'。$ address ['street_address'],$ b $'location'=> $
'description'=> $ start_datetime,
'start'=> timeZone'=>'America / Los_Angeles',
),
'end'=>数组(
'dateTime'=> $ end_datetime,
'timeZone'= >'America / Los_Angeles',
),
'attendees'=>数组(
数组('email'=>'< ADMIN EMAIL>)

));

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


解决方案

在验证之前通过实例化 Google_Service_Calendar 来解决自己的问题,如下所示:

  class Google扩展Google_Client {
函数__construct($ params = array()){
parent :: __ construct();

$ client_email ='< SERVICE ACCOUNT EMAIL>';
$ private_key = file_get_contents('< .P12 FILE PATH>');
$ scopes = array('https://www.googleapis.com/auth/calendar');

$ client = new Google_Client();

$ this-> cal = new Google_Service_Calendar($ client);

$ credentials = new Google_Auth_AssertionCredentials(
$ client_email,
$ scopes,
$ private_key
);

$ client-> setAssertionCredentials($ credentials);
if($ client-> getAuth() - > isAccessTokenExpired()){
$ client-> getAuth() - > refreshTokenWithAssertion();
}
}
}

然后在服务中application / controllers / Booking.php就像这样:

  $ this-> google-> cal-> events-> ; insert($ calendar_id,$ event); 


I want to add an event to a particular Google Apps calendar each time that an appointment is booked in my codeigniter app.

So far I've gone through the following steps (from Using OAuth 2.0 for Server to Server Applications):

  1. Add Google API Client Library to application/third_party
  2. Create Service Account in Developer Console with Google Apps Domain-wide Delegation enabled
  3. Store provided .p12 file
  4. Authorize Service Account Client ID in Google Apps Admin Console with scope of https://www.googleapis.com/auth/calendar
  5. Share calendar with Service Account email address

I'm getting the error "Error calling POST <calendar API insert endpoint> (401) Login Required"

I believe that I must be doing something incorrect in how I'm authenticating. Any advice would be welcomed.

application/libraries/Google.php:

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
set_include_path(APPPATH . 'third_party/' . PATH_SEPARATOR . get_include_path());
require_once APPPATH . 'third_party/Google/autoload.php';
require_once APPPATH . 'third_party/Google/Client.php';

class Google extends Google_Client {
    function __construct($params = array()) {
        parent::__construct();

        $client_email = '<SERVICE ACCOUNT EMAIL>';
        $private_key = file_get_contents('<.P12 FILE PATH>');
        $scopes = array('https://www.googleapis.com/auth/calendar');

        $credentials = new Google_Auth_AssertionCredentials(
            $client_email,
            $scopes,
            $private_key
        );

        $client = new Google_Client();
        $client->setAssertionCredentials($credentials);
        if ($client->getAuth()->isAccessTokenExpired()) {
          $client->getAuth()->refreshTokenWithAssertion();
        }
    }
}

application/controllers/Booking.php:

(Google.php library loaded in constructor)

function addCalendarEvent($user_id, $date, $time, $address, $booking_data) {

    $cal = new Google_Service_Calendar($this->google);

    $calendar_id = '<CALENDAR ID>';

    $start_datetime = date('c', strtotime($date.' '.$time));

    $end_datetime = date('c', strtotime($date.' '.$time.' +2 hours'));

    $event = new Google_Service_Calendar_Event(array(
      'summary' => 'Appointment at '.$address['street_address'],
      'location' => $location,
      'description' => $booking_data['description'],
      'start' => array(
        'dateTime' => $start_datetime,
        'timeZone' => 'America/Los_Angeles',
      ),
      'end' => array(
        'dateTime' => $end_datetime,
        'timeZone' => 'America/Los_Angeles',
      ),
      'attendees' => array(
        array('email' => '<ADMIN EMAIL>')
      )
    ));

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

解决方案

I was able to fix my own problem by instantiating Google_Service_Calendar before authenticating, like so:

class Google extends Google_Client {
    function __construct($params = array()) {
        parent::__construct();

        $client_email = '<SERVICE ACCOUNT EMAIL>';
        $private_key = file_get_contents('<.P12 FILE PATH>');
        $scopes = array('https://www.googleapis.com/auth/calendar');

        $client = new Google_Client();

        $this->cal = new Google_Service_Calendar($client);

        $credentials = new Google_Auth_AssertionCredentials(
            $client_email,
            $scopes,
            $private_key
        );

        $client->setAssertionCredentials($credentials);
        if ($client->getAuth()->isAccessTokenExpired()) {
          $client->getAuth()->refreshTokenWithAssertion();
        }
    }
}

And then using the service in application/controllers/Booking.php like so:

$this->google->cal->events->insert($calendar_id, $event);

这篇关于错误(401)需要登录 - Google Calendar API v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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