Authorizing requests with OAuth 2.0 in Google Spreadsheet API

扶醉桌前 提交于 2019-11-30 22:37:45

What you're actually trying to accomplish is Server to Server authentication between your server and Google.

This way, when a visitor enters your page/s you'll grab data from your own spreadsheet, without any 3rd party involvement.

I'ts possible you'll find what you're looking for in Google Service Account, and here

Also, another solution (which is much easier to accomplish, but might have some set backs) is to use the oauth 2.0 protocol with your Google dev account (retrieved from Google Console API).

  1. If you haven't already, create a Google Dev account (Google Console API)
  2. Generate a access/refresh token for your application with "offline" grant - meaning you can make API requests with your dev account to your spreadsheet account even when you're not logged in with your spreadsheet account.
  3. Save the refresh token you generated and use it to generate access token over and over again (access tokens last 1 hour).

Refresh token are not supposed to expire, but in case it would, you can always generate it again and replace the one you had with a new one, and keep generating access tokens with it.

The major set back is in case your refresh token gets invalidated, you'll have to manually replace it, as it will require you to re-grant access to your dev account to access your spreadsheet account.

I hope this helps a bit.

Meny

Google & OAuth tutorial: I searched for days to find this. It is far better then any other OAuth style tutorial I have used. It is for connecting to Google Docs/google drive.

Python example is here, also see other examples for java etc: https://developers.google.com/drive/examples/python

Note you need to add refresh token to it. But that works just as you would expect.

Also to connect to the spreadsheet use:

          SpreadsheetService service =
                  new SpreadsheetService("MySpreadsheetIntegration-v1");

          service.setHeader("Authorization", "Bearer " + accessToken);

yes, OAuth protocol means that when you will try to ask protected resources from other side (google), your site should redirect your user to other's side site, showing him google's login/password dialog, with a request for confirmation that user is agreed to allow your site to use user's resourses from the other site (google in your case). That is how OAuth is working

and google needs yours user's credentials because google is not sure that user is exactly this user (if he does not have any cookies for example)

You can use the new provided oauth2 flow

//flow use httpTransport, clientSecrets, json factory and datastore factory
val flow = new GoogleAuthorizationCodeFlow
  .Builder(httpTransport,JSON_FACTORY,clientSecrets,SCOPES)
  .setDataStoreFactory(datastoreFactory)
  .build()

// authorize
val credential=new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user")

and use it with your gdata service:

val service:SpreadsheetService=new SpreadsheetService("SpreadsheetIntegration") 
 service.setOAuth2Credentials(credential)

full example in scala: https://github.com/spaced/spreadsheet-oauth2-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!