Hi,
I am working on a web application that needs to send data to a non-public
google spreadsheet, so I need to implement OAuth2 for authorization and
authentication. After some research I managed to authorize using OAuth2.
Here's my sample code in python.
from oauth2client.file import Storage
from oauth2client.tools import run_flow, argparser
from oauth2client.client import OAuth2WebServerFlow
import json
import gdata.spreadsheets.client
import gdata.spreadsheets.data
import gdata.gauth
import flask
app = flask.Flask(__name__)
@app.route('/')
def index():
storage = Storage("creds.dat")
credentials = storage.get()
if credentials is None:
CLIENT_ID = 'XXXXX'
CLIENT_SECRET = 'YYYYYY'
flow = OAuth2WebServerFlow(
client_id = CLIENT_ID,
client_secret = CLIENT_SECRET,
scope = 'https://spreadsheets.google.com/feeds',
)
credentials = run_flow(flow, storage, argparser.parse_args([]))
gd_client = gdata.spreadsheets.client.SpreadsheetsClient()
gd_client.auth_token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
print
gd_client.GetWorksheets('1kQYXDck9IE4B2vcfvx79rxJSIWx0WKjLMvj9jmfKF58').title.text
entry = gdata.spreadsheets.data.ListEntry()
entry.set_value('jobid', '456')
entry.set_value('searchquery', 'test2')
entry.set_value('url', 'http://example2.com')
gd_client.add_list_entry(entry, 'SPREADSHEET_ID', 'WORKSHEET_ID')
return 'OK'
if __name__ == '__main__':
import uuid
app.secret_key = str(uuid.uuid4())
app.debug = True
app.run(host='0.0.0.0', port=8080)
Using this sample code, I was able to use google spreadsheets api and add
new entries in the specified spreadsheet.
My problem now is that access_token expires after one hour, so I've read
that I need to use refresh token instead. In this post
http://stackoverflow.com/questions/7964711/authorizing-requests-with-oauth-2-0-in-google-spreadsheet-api
in the accepted answer, it is mentioned that I should generate a refresh
token, but question is how?
I was also wondering maybe I am in the wrong direction and I should use
Server to Server authentication
https://developers.google.com/identity/protocols/OAuth2ServiceAccount
instead? Because I have a web form, where different users submit their
answers and I want to save their entries in a common spreadsheet. If Server
to Server Application is my case, then question is if I need a Google Apps
domain. I got stuck in this section "Delegating domain-wide authority to
the service account" where there was a link to the Admin console and I
figured out that I need to buy a google app domain. Is this step mandatory
or I can still use Service account from the developer console without
google app domain?
Thanks,
Panagiota
--
You received this message because you are subscribed to the Google Groups
"Google Spreadsheets API" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.