Request authorization using OAuth 2.0 in the Google Spreadsheet API

I am trying to create a PHP web page that requires reading some data from google spreadsheet in my domain (I use Google Apps Free Edition).

The spreadsheet that needs to be read is non-public, but visible to some people in my domain. Since it is not publicly available, I know that there will be some means of authentication and authorization, even if I use the API to read it.

I found this page, but there is something that I do not understand: http://code.google.com/intl/zh-TW/apis/spreadsheets/data/3.0/developers_guide.html#Auth

It says that we should use the OAuth 2.0 protocol, this is normal. But he also says that during the authorization process, "Google displays an OAuth dialog box for the user, prompting them to allow your application to request some of their data." .

My web page will display some data read from a spreadsheet. So, whenever someone goes to my web page, does he display a dialog with the spreadsheet owner asking for permission? Is that what it means?

Any advice would be very welcome.

+4
source share
4 answers

What you are actually trying to accomplish is authentication between the server and Google.

Thus, when a visitor visits your pages, you will capture data from your own table without the participation of a third party.

You may find what you are looking for, a Google service account and here

In addition, another solution (which is much simpler to execute, but may have some delays) is to use the oauth 2.0 protocol with your Google dev account (obtained from the Google Console API).

  • If you haven’t already done so, create a Google Dev account (Google Console API)
  • Create an access / update token for your application with an offline grant - this means that you can send API requests with your account to your spreadsheet, even if you are not logged in with your spreadsheet account.
  • Save the updated token that you created and use it to create the access token again and again (access tokens last 1 hour).

Refreshing the token should not expire, but in case of it, you can always generate it again and replace the one you had with the new one and save access tokens with it.

The basic set in case your update token is invalid, you will have to manually replace it, since you will need to re-grant access to your developer account in order to access your spreadsheet account.

Hope this helps a bit.

Meny

+4
source

Google and OAuth Tutorial: I searched a few days to find this. This is much better than any other OAuth style tutorial I have used. It is designed to connect to Google Docs / Google Drive.

Here is a Python example, as well as other examples for java, etc. https://developers.google.com/drive/examples/python

Please note that you need to add an update token. But it works as you expected.

Also to connect to the spreadsheet, use:

SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1"); service.setHeader("Authorization", "Bearer " + accessToken); 
+4
source

yes, the OAuth protocol means that when you try to request secure resources from the other side (google), your site should redirect the user to another side site, showing him the google login / password dialog, asking for confirmation that the user agrees to allow your site to use user resources from another site (Google in your case). This is how OAuth works.

and google requires your user credentials because Google is not sure that the user is that user (if he does not have cookies, for example)

+2
source

You can use the new oauth2 stream provided

 //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 in the gdata service:

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

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

+2
source

Source: https://habr.com/ru/post/1379106/


All Articles