首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OAuth2Decorator:使用开发者的token为用户运行接口调用

OAuth2Decorator:使用开发者的token为用户运行接口调用
EN

Stack Overflow用户
提问于 2012-07-15 00:21:40
回答 2查看 985关注 0票数 0

对于“正常”的oauth2舞蹈,我需要指定用户并获得相应的令牌。这允许我伪装成该用户的API调用,即代表他。

它还允许用户伪装成我进行调用。一个用例是bigquery,其中我不必向用户授予表访问权限,并且我可以指定我自己的首选控制级别。

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

一般来说,最佳实践是什么?使用正确的oauth (包括流、凭证和存储)?或者使用OAuth2Decorator。

非常感谢。

EN

回答 2

Stack Overflow用户

发布于 2012-07-17 00:20:47

您当然可以使用OAuth2Decorator

下面是一个示例:

main.py

代码语言:javascript
复制
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()

导入到处理BigQuery操作的main.py中的bqclient.py

代码语言:javascript
复制
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中

代码语言:javascript
复制
{
    "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中:

代码语言:javascript
复制
- url: /oauth2callback
  script: oauth2client/appengine.py

希望这能有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2012-07-24 05:01:11

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

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

另外,请参阅App Engine数据存储到BigQuery代码实验室的this partthis part,该代码也使用此授权方法。

代码可能如下所示:

代码语言:javascript
复制
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')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11485392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档