试图从googledfa报告工具获取数据?

2024-04-24 15:56:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从Google获取oauth,每次认证后我都需要复制并粘贴控制台中生成的代码! 我想让它自动化? 你知道我怎么做吗?在

我有这个密码!在

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参数? 这项服务在谷歌云上是免费的吗?在


Tags: namehttpsselfcomurlwwwuriflow
1条回答
网友
1楼 · 发布于 2024-04-24 15:56:37

请尝试以下操作-它应该在第一次提示您,但随后将凭据写入磁盘并读入以备后续调用:

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)

相关问题 更多 >