1

我正在尝试将Gdata Spreadsheet API与OAuth2配合使用。使用带有证书验证的电子表格API OAuth2

使用OAuth2.0的客户端ID可与OAuth2WebServerFlow域上, 但使用服务帐户/证书导致使用的400错误的请求

的范围是

https://www.googleapis.com/auth/userinfo.email 
https://www.googleapis.com/auth/userinfo.profile 
https://www.googleapis.com/auth/admin.directory.group.readonly 
https://www.googleapis.com/auth/admin.directory.user.readonly 
https://docs.google.com/feeds/ 
https://spreadsheets.google.com/feeds 

enter image description here enter image description here

这里是我使用的代码

成功:OAuth2.0的

flow = OAuth2WebServerFlow(client_id=CLIENT_ID, 
          client_secret=CLIENT_SECRET, 
          scope=SCOPE, 
          access_type = "online", 
          redirect_uri=REDIRECT_URI) 
credentials = flow.step2_exchange(code) 
auth2token = gauth.OAuth2Token(
          client_id=credentials.client_id, 
          client_secret=credentials.client_secret, 
          scope=SCOPE, 
          access_token=credentials.access_token, 
          refresh_token=credentials.refresh_token, 
          user_agent='spreadsheetclient/1.0',) 
client = SpreadsheetsClient(auth_token=auth2token) 
auth2token.authorize(client) 
q = SpreadsheetQuery(title= "ItemMaster",title_exact=True) 
feed = client.get_spreadsheets(query = q) 

失败OAuth2.0的证书

credentials = SignedJwtAssertionCredentials(
         SERVICE_ACCOUNT_EMAIL, 
         CERTIFICATE, 
         scope = SCOPE, 
         prn = "[email protected]" 
         ) 
http = httplib2.Http() 
http = credentials.authorize(http) 
auth2token = gauth.OAuth2Token(
         client_id=credentials.client_id, 
         client_secret=credentials.client_secret, 
         scope=SCOPE, 
         access_token=credentials.access_token, 
         refresh_token=credentials.refresh_token, 
         user_agent='spreadsheetclient/1.0',) 
client = SpreadsheetsClient() 
auth2token.authorize(client) 
q = SpreadsheetQuery(title= "ItemMaster",title_exact=True,) 
feed = client.get_spreadsheets(query = q) 

那么,有没有一种方式来获得证书认证为GDATA API工作?

回答

4

我解决它通过使用OAuth2TokenFromCredentials

credentials = SignedJwtAssertionCredentials(
        SERVICE_ACCOUNT_EMAIL, 
        PRIVATE_KEY, 
        scope = SCOPE, 
        sub = "[email protected]") 
    auth2token = gauth.OAuth2TokenFromCredentials(credentials) 
    client = SpreadsheetsClient() 
    auth2token.authorize(client) 
    q = SpreadsheetQuery(title= "ItemMaster",title_exact=True,) 
    feed = client.get_spreadsheets(query = q) 
    self.response.write(feed)