0

"정상적인"oauth2 댄스의 경우 사용자를 지정하고 해당 토큰을 얻습니다. 이렇게하면 API 호출을 해당 사용자로 위장 할 수 있습니다 (즉, 사용자를 대신하여).OAuth2Decorator : 개발자 토큰을 사용하여 사용자를위한 API 호출을 실행하십시오.

사용자가 내게 가장하는 전화를 걸도록 허용 할 수도 있습니다. 유스 케이스는 사용자에게 테이블 액세스 권한을 부여 할 필요가없는 bigquery이며 사용자가 선호하는 제어 수준을 지정할 수 있습니다.

단순화 된 OAuth2Decorator를 사용하면이 옵션이없는 것 같습니다. 내가 말할 권리가 있니? 해결 방법이 있습니까?

일반적으로 가장 좋은 방법은 무엇입니까? 적절한 oauth (흐름, 자격 증명 및 저장소로 구성)를 사용하려면? 또는 OAuth2Decorator를 사용합니다.

대단히 감사합니다.

+0

이 [페이지] :

코드는 다음과 같이 보일 수 있습니다 oauth를 사용하여 토큰을 얻는 방법을 보여줍니다. 이 액세스 토큰 또는 새로 고침 토큰입니까? 누구든지 새로 고침 토큰을 받고 새 액세스 토큰을 얻기 위해 샘플 코드를 가지고 있습니까? – user918081

답변

2

당신은 확실히 OAuth2Decorator 여기

를 사용할 수는 예입니다 BigQuery의 작업을 처리하여 main.py에서 수입됩니다

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

main.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" 
    } 
} 

, 당신의 애플리케이션 제목에 다음 줄을 추가하는 것을 잊지 마세요 : 도움이

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

희망을.

0

사용 사례를 완전히 이해하고 있는지 잘 모르겠지만 다른 사용자가 자신의 자격 증명을 기반으로 액세스 권한을 부여하지 않고 사용하도록 응용 프로그램을 만드는 경우 App Engine 서비스 계정을 사용하는 것이 좋습니다.

이 인증 흐름 유형의 예는 App Engine service accounts + Prediction API article에 설명되어 있습니다.

또한이 인증 방법을 사용하는 App Engine Datastore 대 BigQuery 코드 테이블의 및 this part을 참조하십시오. (https://code.google.com/p/google-api-python-client/wiki/OAuth2) 가정 또한

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') 
관련 문제