栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

AccessTokenRefreshError:带有App Engine App的oAuth 2.0服务帐户的Google Spreadsheet API

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AccessTokenRefreshError:带有App Engine App的oAuth 2.0服务帐户的Google Spreadsheet API

我发现,不

SignedJwtAssertionCredentials
sub
参数调用(对于“模拟”用户)不会产生
AccessTokenRefreshError:access_denied

import osimport httplib2from google.appengine.api import memcachefrom apiclient.discovery import buildfrom oauth2client.client import SignedJwtAssertionCredentialsimport gdata.spreadsheets.clientimport gdata.spreadsheet.service# AppAssertionCredentials is not supported in gdata python client library,# so we use SignedJwtAssertionCredentials with the credential # file of this service account.# Load the key in PKCS 12 format that you downloaded from the Google API# Console when you created your Service account.clientEmail = '10346........-g3dp......................3m1em8@developer.gserviceaccount.com'p12File = 'app.p12'path = os.path.join(ROOT_DIR, 'data', 'oAuth2', p12File)impersonateUser = 'user@mygoogleappsdomain.com'spreadsheetKey = '1mcJHJ...................................juQMw' # ID copied from URL of desired spreadsheet in Google Drivewith open(path) as f:    privateKey = f.read()    f.close()# Getting credentials with AppAssertionCredentials only worked successfully# for Google API Client Library for Python, e.g. accessing file's meta-data.# So we use SignedJwtAssertionCredentials, as suggested in# http://stackoverflow.com/questions/16026286/using-oauth2-with-service-account-on-gdata-in-python# but with-out the sub parameter!credentials = SignedJwtAssertionCredentials(    clientEmail,    privateKey,    scope=(        'https://www.googleapis.com/auth/drive.file ',        # added the scope above as suggested somewhere else,        # but error occurs with and with-out this scope        'https://www.googleapis.com/auth/drive',        'https://spreadsheets.google.com/feeds',        'https://docs.google.com/feeds'    ),#    sub=impersonateUser)http = httplib2.Http()http = credentials.authorize(http)auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)# this pattern would eventually also work using SpreadsheetsService()# SpreadsheetsService methods are different from SpreadsheetsClient, though#srv = gdata.spreadsheet.service.SpreadsheetsService()#srv = auth2token.authorize(srv)clt = gdata.spreadsheets.client.SpreadsheetsClient()clt = auth2token.authorize(clt)wks = clt.get_worksheets(spreadsheetKey)# works now!


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/652788.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号