Search code examples
google-sheetsgoogle-apigoogle-oauthgoogle-sheets-api

How to login to the Google API via console and make POST and GET requests to work with Spread sheets?


I need to work with Google Spread sheets data via console - read the rows, edit them, delete them, etc. On OAuth 2.0 Playground everything works fine. The first task, as I understood it, get a token. The second is how to use token to make a request for editing sheets. I see a lot of examples how to do this on Node.js, Android, C#, but I do not find it anywhere to do it in the browser by POST/GET or console.


Solution

  • The first thing you will need to do is get an access token. The access token can then be tacked onto any of your requests to the sheets api

    access_token=
    

    The following is a quick reference example of three legged OAuth2 request to Google. Note: client_id, redirect_uri, client_secret are all values that you have set up for your app in Google Developers Console. Scope will depend upon which Google Api you would like to access, more then one can be separated by a comma. I will be using the scope for Google Analytics in this example.

    The initial URL to request that the user give you access to there account should look like this: Note: response_type=code

    https://accounts.google.com/o/oauth2/auth?client_id={clientid}.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/analytics.readonly&response_type=code
    

    This will return a Authentication Code, it is used to request a refresh token. It is displayed to the user in the body of the html as well as in the title of the page. To get a Refresh Token you POST the Authentication code back to Google. Note: This is a HTTP Post you cant just place it in a browser that would be a HTTP Get. Note: grant_type=authorization_code

    https://accounts.google.com/o/oauth2/token
    code=4/X9lG6uWd8-MMJPElWggHZRzyFKtp.QubAT_P-GEwePvB8fYmgkJzntDnaiAI&client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code
    

    This is the response:

    {
    "access_token" : "ya29.1.AADtN_VSBMC2Ga2lhxsTKjVQ_ROco8VbD6h01aj4PcKHLm6qvHbNtn-_BIzXMw",
    "token_type" : "Bearer",
    "expires_in" : 3600,
    "refresh_token" : "1/J-3zPA8XR1o_cXebV9sDKn_f5MTqaFhKFxH-3PUPiJ4"
    }
    

    The access_token you get from the above request is what you will be using to make requests to the service. After one hour your access token will have expired you will need to request a new access_token you take the refresh_token that you got above and HTTP Post it to: Note: grant_type=refresh_token

    https://accounts.google.com/o/oauth2/token
    client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&refresh_token=1/ffYmfI0sjR54Ft9oupubLzrJhD1hZS5tWQcyAvNECCA&grant_type=refresh_token
    

    This is the response:

    {
    "access_token" : "ya29.1.AADtN_XK16As2ZHlScqOxGtntIlevNcasMSPwGiE3pe5ANZfrmJTcsI3ZtAjv4sDrPDRnQ",
    "token_type" : "Bearer",
    "expires_in" : 3600
    }
    

    My full tutorial on this can be found here