How to get an access token from Google without an api library?

I am working on an Elixir Phoenix web project where I want to interact with Google's Indexing API.

Google uses OAuth2 to authenticate api requests and actually has a decent documentation on this.

But it only explains the process using one of the supported libraries in Python, Java, PHP or JS.

I would like to make the HTTP requests by myself to retrieve that access token. But the request format (including headers or parameters) is nowhere documented and I cannot even figure out from the libraries' source code.

I have tried requesting https://accounts.google.com/o/oauth2/token (also other eligible URLs) in Postman with the "OAuth 2.0" request type. But it was all just guessing and trying. All the research did not help.

1 answer

  • answered 2019-08-06 22:20 Adam Millerchip

    There are useful instructions including HTTP/Rest examples at Using OAuth 2.0 for Web Server Applications. Each step has the individual parameters fully documented. Here are some useful excerpts.

    Send user to Google's OAuth 2.0 server. Example URL:

    https://accounts.google.com/o/oauth2/v2/auth?
     scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly&
     access_type=offline&
     include_granted_scopes=true&
     state=state_parameter_passthrough_value&
     redirect_uri=http%3A%2F%2Foauth2.example.com%2Fcallback&
     response_type=code&
     client_id=client_id
    

    Retreive authorization code (your domain). Example:

    https://oauth2.example.com/auth?code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7
    

    Request access token. Example:

    POST /oauth2/v4/token HTTP/1.1
    Host: www.googleapis.com
    Content-Type: application/x-www-form-urlencoded
    
    code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
    client_id=your_client_id&
    client_secret=your_client_secret&
    redirect_uri=https://oauth2.example.com/code&
    grant_type=authorization_code
    

    Use API. Example:

    GET /drive/v2/files HTTP/1.1
    Authorization: Bearer <access_token>
    Host: www.googleapis.com/