0
votes

I'm trying to make some Rest-API Calls to the Google Calendar API from my Application to show some Calendar Data and I'm slowly but surely a little in despair.

I've activated the API, created Credentials and downloaded the client-secret Json File. For testing I used Postman to send an example Request to the api like https://www.googleapis.com/calendar/v3/calendars/{calendar-ID}/acl

Because there was no Access Token to in the json file I used "Get new Access Token", entered all the Data from the json and got a Key. But if I use this Key for sending a Request with oAuth2 Authorization and that example API-Call i get a back a 401.

I played around with this for hours without any success. I'm not very experienced with this kind of Authorization and surely missing a point here.

Actually I just want to make some cUrls from my php-script to get some Calendar Data. Maybe someone could describe the steps that are nessecary to get this done.

thank you in advance. :)

2

2 Answers

0
votes

The credentials file that you downloaded from google identifies your application to google. It does not give you direct access to an api.

In order to access a google api containing private user data you need to be authenticated using OAuth2 to gain consent of the resource owner to accesses their data.

These links may help you set up postman using the client id and client secret you have from Google developer console.

0
votes

One problem left. Using the access token to get Data via Postman works in between. Also using the refresh-token to get a new acces token. But in my Curl, when doing the api-request, i still get a 401 back. Refreshing the access token via curl works pretty well, but the actual api-call for getting data seems still not to be on point.

  $oCurl = curl_init();
    curl_setopt($oCurl, CURLOPT_HTTPHEADER, array(
        'Authorization' => 'Bearer '.$token,

    ));

    curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($oCurl, CURLOPT_TIMEOUT, 30);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($oCurl, CURLOPT_URL, $url);
    $response = curl_exec($oCurl);
    $list = json_decode($response );

so Response is still

[code] => 401
[message] => Login Required

maybe someone has a clue whats wrong here? :)

€ okay, changing 'Authorization' => 'Bearer '.$token, to 'Authorization: Bearer '.$token, made it.

Thanks for your help