Accessing a Google Drive spreadsheet from Appengine

后端 未结 2 1885

I have an appengine app that needs to access a single, hard-coded spreadsheet on Google Drive.

Up until now I have been achieving this as follows:

         


        
2条回答
  •  失恋的感觉
    2021-01-14 05:05

    EDIT: As Google have redesigned the API Console, the details of the steps below have changed - see comments

    OK here goes, step by step

    1. Go to Google Cloud Console and register your project (aka application)
    2. You need to note the Client ID, and Client Secret
    3. Go to the OAuth Playground, click the gear icon and choose and enter your own credentials
    4. You will be reminded that you need to go back to the Cloud COnsole and add the Oauth Playground as a valid callback url. So do that.
    5. Do Step 1, choosing the spreadsheet scope and click authorize
    6. Choose your Google account if prompted and grant auth when prompted
    7. Do Step 2, Click 'Exchange auth code for tokens'
    8. You will see an input box containing a refresh token

    The refresh token is the equivalent of your long lived username/password, so this is what you'll hard code (or store someplace secure your app can retrieve it).

    When you need to access Google Spreadsheets, you will call

    POST https://accounts.google.com/o/oauth2/token
    content-type: application/x-www-form-urlencoded
    client_secret=************&grant_type=refresh_token&refresh_token=1%2xxxxxxxxxx&client_id=999999999999.apps.googleusercontent.com
    

    which will return you an access token

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

    Put the access token into an http header for whenever you access the spreadsheet API

    Authorization: Bearer ya29.yyyyyyyyyyyyyyyyy
    

    And you're done

提交回复
热议问题