【问题标题】:Google DataLab oauth2.0 link & redirect not authenticating notebookGoogle DataLab oauth2.0 链接和重定向未验证笔记本
【发布时间】:2017-06-02 02:18:11
【问题描述】:

我正在尝试将 Google Analytics(分析)数据导入 Google Cloud DataLab,但其中的 oauth2 部分遇到了问题。

我基本上关注this quick start,我让它在我的终端上工作,但后来我把它移到 Cloud Datalab 我运行代码并收到这条消息:

Your browser has been opened to visit:

https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.readonly&redirect_uri=http%3A%2F%2Flocalhost%3A8090%2F&response_type=code&client_id=xxxxxxxxxxxx.apps.googleusercontent.com&access_type=offline

If your browser is on a different machine then exit and re-run this
application with the command-line parameter 

  --noauth_local_webserver

我单击链接并通过身份验证过程并允许访问,但它让我回到 404 的 localhost url: http://localhost:8090/?code=4/xxxxxxxxxxxxxxx2C-WoxViSyyyyyyyyyxMsvGgs#

并且从不授予对数据实验室笔记本的访问权限。

如何让它将我重定向回正确的位置或验证该笔记本? 或者我应该使用另一种 oauth 方法吗?

这是我正在运行的所有代码:

import argparse

from apiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools

def get_service(api_name, api_version, scope, client_secrets_path):
    """Get a service that communicates to a Google API.

    Args:
    api_name: string The name of the api to connect to.
    api_version: string The api version to connect to.
    scope: A list of strings representing the auth scopes to authorize for the
        connection.
    client_secrets_path: string A path to a valid client secrets file.

    Returns:
    A service that is connected to the specified API.
    """
    # Parse command-line arguments.
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        parents=[tools.argparser])
    flags = parser.parse_args([])

    # Set up a Flow object to be used if we need to authenticate.
    flow = client.flow_from_clientsecrets(
        client_secrets_path, scope=scope,
        message=tools.message_if_missing(client_secrets_path))

    # Prepare credentials, and authorize HTTP object with them.
    # If the credentials don't exist or are invalid run through the native client
    # flow. The Storage object will ensure that if successful the good
    # credentials will get written back to a file.
    storage = file.Storage(api_name + '.dat')
    credentials = storage.get()
    if credentials is None or credentials.invalid:
        credentials = tools.run_flow(flow, storage, flags)
    http = credentials.authorize(http=httplib2.Http())
    # Build the service object.
    service = build(api_name, api_version, http=http)
    return service

def get_results(service, profile_id):
    # Use the Analytics Service Object to query the Core Reporting API
    # for the number of sessions in the past seven days.
    return service.data().ga().get(
        ids='ga:' + profile_id,
        start_date='7daysAgo',
        end_date='today',
        metrics='ga:sessions').execute()

def print_results(results):
    # Print data nicely for the user.
    if results:
        print ('View (Profile): %s' % results.get('profileInfo').get('profileName'))
        print ('Total Sessions: %s' % results.get('rows')[0][0])

    else:
        print ('No results found')

def get_data():
    # Define the auth scopes to request.
    scope = ['https://www.googleapis.com/auth/analytics.readonly']

    # Authenticate and construct service.
    service = get_service('analytics', 'v3', scope, 'client_secrets.json')
    profile = '75633300' #get_first_profile_id(service)
    print_results(get_results(service, profile))

get_data()

【问题讨论】:

  • 我也面临同样的问题,你能从datalab连接到google-analytics吗??

标签: google-analytics-api google-authentication google-cloud-datalab


【解决方案1】:

Here 就是答案!

craigcitro 于 2017 年 6 月 10 日发表评论

这里的问题正是@ncouture 猜到的——你没有将任何参数传递给 parse_args,所以它试图解析 sys.argv。

尝试调用 argparser.parse_args([]),或根据需要添加其他参数。

所以你只需使用 flags = parser.parse_args(['--noauth_local_webserver']) 执行“创建服务”

【讨论】:

  • 这个答案可以通过一个小代码示例进行改进,以准确显示您的意思。 (不要依赖链接总是指向未来有用的信息;在这里放一个完整的答案更好。)stackoverflow.com/help/how-to-answer
猜你喜欢
  • 1970-01-01
  • 2019-08-16
  • 2019-03-28
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多