将新事件添加到Google日历Google_Service_Exception(401)时出错[{错误“:” unauthorized_client“,” error_description“:”未经授权“}

时间:2019-06-26 12:18:31

标签: laravel google-calendar-api google-plus

当我尝试在laravel项目中向Google日历提交新事件时,我总是会遇到此错误:

  

Google_Service_Exception(401)   {“ error”:“ unauthorized_client”,“ error_description”:“ Unauthorized”}

我为日历api创建了新的OAuth凭据,并将其添加到.env文件中,如下所示:

.env

我还使用google + API,以便每个用户都可以通过OAuth 2访问其日历以更新其事件

我尝试添加新的OAuth凭据,但问题仍然存在,我还尝试通过将添加到.env文件和范围中的客户端ID添加并授权它们来将域范围的权限委派给我的服务帐户,但我没有等待任何更改等待更改发生的24小时,但什么都没有改变

以下是用于创建新事件的函数:

public function doCreateEvent(Event $evt, Request $request)
{
    $this->validate($request, [
        'title' => 'required',
        'calendar_id' => 'required',
        'datetime_start' => 'required|date',
        'datetime_end' => 'required|date'
    ]);

    $title = $request->input('title');
    $calendar_id = $request->input('calendar_id');
    $start = $request->input('datetime_start');
    $end = $request->input('datetime_end');

    $start_datetime = Carbon::createFromFormat('Y/m/d H:i', $start);
    $end_datetime = Carbon::createFromFormat('Y/m/d H:i', $end);

    $cal = new \Google_Service_Calendar($this->client);
    $event = new \Google_Service_Calendar_Event();
    $event->setSummary($title);

    $start = new \Google_Service_Calendar_EventDateTime();
    $start->setDateTime($start_datetime->toAtomString());
    $event->setStart($start);
    $end = new \Google_Service_Calendar_EventDateTime();
    $end->setDateTime($end_datetime->toAtomString());
    $event->setEnd($end);

    // Create new conference
    $conference = new \Google_Service_Calendar_ConferenceData();

    $entryPoint = new \Google_Service_Calendar_EntryPoint();
    $entryPoint->setAccessCode('wx12z3s');
    $entryPoint->setEntryPointType('video');
    $entryPoint->setLabel('meet.google.com/wx12z3s');
    $entryPoint->setMeetingCode('wx12z3s');
    $entryPoint->setPasscode('wx12z3s');
    $entryPoint->setPassword('wx12z3s');
    $entryPoint->setPin('wx12z3s');
    $entryPoint->setUri('https://meet.google.com/wx12z3s');

    $conference->setEntryPoints($entryPoint);

    $conferenceSolution = new \Google_Service_Calendar_ConferenceSolution();
    $conferenceSolution->setIconUri(null);
    $conferenceSolution->setKey(new \Google_Service_Calendar_ConferenceSolutionKey());

    $conference->setConferenceSolution($conferenceSolution);

    $conferenceRequest = new \Google_Service_Calendar_CreateConferenceRequest();
    $conferenceRequest->setRequestId($request->_token);
    $conferenceSolutionKey = new \Google_Service_Calendar_ConferenceSolutionKey();

    $conferenceSolutionKey->setType("hangoutsMeet");
    $conferenceRequest->setConferenceSolutionKey($conferenceSolutionKey);
    $conferenceRequest->setStatus(new \Google_Service_Calendar_ConferenceRequestStatus());

    $conference->setCreateRequest($conferenceRequest);

    $event->setConferenceData($conference);

    //attendee
    if ($request->has('attendee_name')) {
        $attendees = [];
        $attendee_names = $request->input('attendee_name');
        $attendee_emails = $request->input('attendee_email');

        foreach ($attendee_names as $index => $attendee_name) {
            $attendee_email = $attendee_emails[$index];
            if (!empty($attendee_name) && !empty($attendee_email)) {
                $attendee = new \Google_Service_Calendar_EventAttendee();
                $attendee->setEmail($attendee_email);
                $attendee->setDisplayName($attendee_name);
                $attendees[] = $attendee;
            }
        }

        $event->attendees = $attendees;
    }

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

    $evt->title = $title;
    $evt->calendar_id = $calendar_id;
    $evt->event_id = $created_event->id;
    $evt->datetime_start = $start_datetime->toDateTimeString();
    $evt->datetime_end = $end_datetime->toDateTimeString();
    $evt->save();

    return redirect('/event/create')
                ->with('message', [
                    'type' => 'success',
                    'text' => 'Event was created!'
                ]);
}

我正在使用G Suite帐户,以便我可以添加事件并为其分配视频群聊会议会议,但是当我尝试将新创建的事件添加到用户日历时,问题始终显示

1 个答案:

答案 0 :(得分:0)

问题是我试图用来访问用户日历的访问令牌是错误的。当我删除所有用户数据时,当然他的访问令牌已保存到数据库中,并尝试再次登录,以便使用新的访问令牌为用户创建新记录,问题解决了,我现在可以访问他的日历并创建新事件< / p>