2013-12-20 18 views
2

我試圖從谷歌獲得oauth,每次我需要複製並粘貼我的控制檯中生成的代碼後進行身份驗證! 我想讓它自動化嗎? 任何想法我怎麼能做到這一點?試圖從Google DFA報表工具中獲取數據?

我有這段代碼!

DFA_USER_PROFILE_NAME='MYPROFILE_NAME'

scope = ["https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/dfareporting"]

try: 

    redirect_uri=redirect_uri='urn:ietf:wg:oauth:2.0:oob' 
    flow=flow_from_clientsecrets('Location of the json file', scope, redirect_uri, message) 
    authorize_url = flow.step1_get_authorize_url() 
    self.redirect(authorize_url) 
    credential = flow.step2_exchange(self.request.params) 
    storage=StorageByKeyName(credentials,user.user_id(),'credentials' 
         ) 
    storage.put(credentials) 
    user=user.get_current_user() 

    credentials = client.oauth2credentials 
    client.oauth2credentials = credentials 
    http = httplib2.Http() 

except BaseException as e: print "this is really a error that has occured ",e

我想,但我不知道我要去的地方錯了嗎?

什麼是self.params? Google Cloud中免費提供此服務嗎?

回答

2

試試下面的 - 它應該提示你的第一次,但隨後寫了憑據盤和閱讀他們在以後的調用:

import httplib2 
from oauth2client.file import Storage 
from oauth2client.client import flow_from_clientsecrets 
from oauth2client.tools import run_flow, argparser 
from apiclient.discovery import build 

storage = Storage("/path/to/saved_user_creds.dat") 
credentials = storage.get() 
if credentials is None or credentials.invalid: 
    credentials = run_flow(flow_from_clientsecrets("/path/to/client_secrets.json", scope=["scope1" ,"scope2"]), storage, argparser.parse_args([])) 
http = credentials.authorize(httplib2.Http()) 

# Use the http object as needed... 
service = build("bigquery", "v1") 
result = service.object().method(name=value).execute(http=http) 
+0

我們有** PUT方法**我們如何使用代碼在這裏? 存儲證書並使用** GET **方法。 如何創建.dat文件?在這種情況下 ? – rshvkmr

+0

.dat文件在第一次運行時自動爲您創建。隨意忽略最後兩行,只需使用http對象,它應該適用於GET和PUT請求。 – aeijdenberg