Search code examples
oauth-2.0google-apigoogle-calendar-api

Connect to google api without library


I would like to use the Google Calendar API. To do this I need an authorization token, which I want to obtain using simple http requests. So, I have been following this: https://developers.google.com/accounts/docs/OAuth2InstalledApp#formingtheurl

I try the following request:

POST https://accounts.google.com/o/oauth2/auth

grant_type: authorization_code
scope: https://www.googleapis.com/auth/calendar
response_type: code
redirect_uri: http://localhost:1337
client_id: id

However, from this request i get the response:

Error:invalid_request
Required parameter is missing: response_type

I have also tried countless of other parameters, urls, request methods and different applications for sending the request, however, i do always get the same result. Furthermore I have also tried to re-create new projects and new clientsIDs in the google console, but that does not seem to help either.

I might simply do some fundamental mistake, idk, the documentation seems somewhat unclear.


Solution

  • Ok, your request doesn't make any sence. This is a mix of the user's (the client) request, to get you an authorization code and the request to get you (the server) the access token from Google (the provider).

    I try to explain, what you have to do:

    • First, make the user to send a GET request to server, like this one:

    GET:

    https://accounts.google.com/o/oauth2/auth?scope={YOUR_SCOPES_URL_ESCAPED}&redirect_uri={YOUR_REDIRECT_URI_URL_ESCAPED}&response_type=code&client_id={YOUR_CLIENT_ID}
    
    • You send a POST request:

    Request:

    POST /o/oauth2/token HTTP/1.1  
    Host: accounts.google.com  
    Content-Type: application/x-www-form-urlencoded  
    
    code={YOUR_RETURNED_CODE_FROM_FIRST_REQUEST}&  
    client_id={YOUR_CLIENT_ID}&  
    client_secret={YOUR_CLIENT_SECRET}&  
    redirect_uri=https://oauth2-login-demo.appspot.com/code&  
    grant_type=authorization_code
    
    • If this has success, you get something like this as result:

    JSON:

    {  
      "access_token":"1/fFAGRNJru1FTz70BzhT3Zg",  
      "expires_in":3920,  
      "token_type":"Bearer",  
      "refresh_token":"1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"  
    }
    

    A good place, to see, what you can do, have a look at the Google OAuth 2.0 Playground.