0

对于“正常”oauth2舞蹈,我可以指定用户并获得相应的令牌。 这让我进行API调用伪装成该用户,即代表他。OAuth2Decorator:使用开发人员令牌为用户运行API调用

它也可以让用户伪装成我。 用例是BigQuery的,我不必授予用户表的访问,我可以指定控制我自己的最佳水平。

使用简化的OAuth2Decorator,我似乎没有这个选项。 难道我说得对吗? 还是有变通?

一般来说,什么是最好的做法是什么?使用适当的oauth(包括Flow,Credentials和Storage)?或者使用OAuth2Decorator。

非常感谢。

+0

此外,该[页码](https://code.google.com/p/google-api-python-client/wiki/OAuth2)按说演示如何使用oauth获取令牌。这是一个访问令牌还是刷新令牌?有没有人有任何示例代码获取刷新令牌并使用它获取新的访问令牌? – user918081 2012-07-15 10:49:28

回答

2

你当然可以使用OAuth2Decorator

下面是一个例子:

main.py是获取您的main.py它处理的BigQuery行动进口

import bqclient 
import httplib2 
import os 

from django.utils import simplejson as json 
from google.appengine.api import memcache 
from google.appengine.ext import webapp 
from google.appengine.ext.webapp.util import run_wsgi_app 
from oauth2client.appengine import oauth2decorator_from_clientsecrets 

PROJECT_ID = "xxxxxxxxxxx" 
DATASET = "your_dataset" 

QUERY = "select columns from dataset.table" 

CLIENT_SECRETS = os.path.join(os.path.dirname(__file__),'client_secrets.json') 

http = httplib2.Http(memcache) 
decorator = oauth2decorator_from_clientsecrets(CLIENT_SECRETS, 
        'https://www.googleapis.com/auth/bigquery') 

bq = bqclient.BigQueryClient(http, decorator) 

class MainHandler(webapp.RequestHandler): 
    @decorator.oauth_required 
    def get(self): 
    data = {'data': json.dumps(bq.Query(QUERY, PROJECT_ID))} 
    template = os.path.join(os.path.dirname(__file__), 'index.html') 
    self.response.out.write(render(template, data)) 

application = webapp.WSGIApplication([('/', MainHandler),], debug=True) 

def main(): 
    run_wsgi_app(application) 

if __name__ == '__main__': 
    main() 

bqclient.py

from apiclient.discovery import build 

class BigQueryClient(object): 
    def __init__(self, http, decorator): 
     """Creates the BigQuery client connection""" 
     self.service = build('bigquery', 'v2', http=http) 
     self.decorator = decorator 

    def Query(self, query, project, timeout_ms=10): 
     query_config = { 
      'query': query, 
      'timeoutMs': timeout_ms 
     } 
     decorated = self.decorator.http() 
     queryReply = (self.service.jobs() 
      .query(projectId=project, body=query_config) 
      .execute(decorated)) 
     jobReference=queryReply['jobReference'] 
     while(not queryReply['jobComplete']): 
      queryReply = self.service.jobs().getQueryResults(
       projectId=jobReference['projectId'], 
       jobId=jobReference['jobId'], 
       timeoutMs=timeout_ms).execute(decorated) 
     return queryReply 

其中所有的验证细节都保存在json文件中的client_secrets.json

{ 
    "web": { 
     "client_id": "xxxxxxxxxxxxxxx", 
     "client_secret": "xxxxxxxxxxxxxxx", 
     "redirect_uris": ["http://localhost:8080/oauth2callback"], 
     "auth_uri": "https://accounts.google.com/o/oauth2/auth", 
     "token_uri": "https://accounts.google.com/o/oauth2/token" 
    } 
} 

最后,不要忘了这些行添加到您的app.yaml:

- url: /oauth2callback 
    script: oauth2client/appengine.py 

希望有所帮助。

0

我不确定我是否完全理解用例,但是如果您正在创建一个应用程序供他人使用,而不必根据他们自己的凭据授权访问,我会推荐使用App Engine服务帐户。

App Engine service accounts + Prediction API article中描述了这种认证流程的一个示例。

此外,请参阅App Engine数据存储的this partthis part到BigQuery codelab,它也使用此授权方法。

的代码可能是这个样子:

import httplib2 

# Available in the google-api-python-client lib 
from apiclient.discovery import build 
from oauth2client.appengine import AppAssertionCredentials 

# BigQuery Scope 
SCOPE = 'https://www.googleapis.com/auth/bigquery' 

# Instantiate and authorize a BigQuery API client 
credentials = AppAssertionCredentials(scope=SCOPE) 
http = credentials.authorize(httplib2.Http()) 
bigquery_service = build("bigquery", "v2", http=http) 

# Make some calls to the API 
jobs = bigquery_service.jobs() 
result = jobs.insert(projectId='some_project_id',body='etc, etc') 
相关问题