【问题标题】:Oauth access to external Google Brand Account对外部 Google 品牌帐号的 Oauth 访问权限
【发布时间】:2020-04-30 03:56:42
【问题描述】:

我的公司有一个Google Brand Account 来管理我们的 YouTube (YT) 频道。我们正在尝试使用 YouTube 分析和报告 API 来自动导出有关我们频道的指标。为此,我们在 GCP 组织中创建了一个应用程序,并在该应用程序中创建了一个 Oauth 客户端。为我们 GCP 组织内的用户拥有的频道检索 YouTube 数据时,一切正常。但是,拥有我们感兴趣的 YT 频道的品牌帐号不是我们 GCP 组织的成员。这意味着当尝试使用我们的 Oauth 客户端访问该频道时,我们会收到以下错误:

Error 403: org_internal
This client is restricted to users within its organization.

通过搜索在线文档,我们似乎可以执行以下操作之一:

  1. 使我们的应用程序在 API 和服务外部 -> OAuth 同意屏幕部分 将品牌帐号迁移到我们的 GCP 组织
  2. 我不知道怎么做 2. 以及它是否真的可行。 1. 对我来说似乎有点过火,因为我们真的不想访问任何拥有 Google 帐户的用户的数据,但这可能是唯一的方法。因此,我正在寻求有关如何以最佳方式进行的帮助,以便我们可以在 GCP 组织中使用 OAuth 客户端从我们的 YT 渠道获取分析数据。

【问题讨论】:

    标签: google-cloud-platform


    【解决方案1】:

    看起来这是第一次记录在 here。它被 Google 标记为“无法修复”,不知道为什么。

    我能够使用this OAuth Playground (OAP) 解决方法来让我的应用正常工作。这是一个非常可悲的解决方法,因为令牌只能工作一个小时,然后您必须在操场上手动刷新。

    从 OAP 获得令牌后,这是我正在使用的代码。

    import os, json
    from pathlib import Path
    import google.oauth2.credentials
    from googleapiclient.discovery import build
    
    # pasted from OAP, note only access_token is actually needed
    Path('oap.json').write_text('''
    {
      "access_token": "tokenstring", 
      "scope": "https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/yt-analytics.readonly", 
      "token_type": "Bearer", 
      "expires_in": 3599, 
      "refresh_token": "refreshtoken"
    }
    ''')
    
    TOKEN_FILE = 'oap.json'
    
    # For Reference
    # SCOPES = ['https://www.googleapis.com/auth/yt-analytics.readonly',
    #           'https://www.googleapis.com/auth/youtube.readonly']
    
    API_SERVICES = [
                    ('youtubeAnalytics', 'v2'),
                    ('youtube', 'v3')
                    ]
    
    oap = json.load(open(TOKEN_FILE, 'r'))
    creds = google.oauth2.credentials.Credentials(oap['access_token'])
    
    service_list = []
    for API_SERVICE_NAME,API_VERSION in api_services:
        service = build(API_SERVICE_NAME, API_VERSION, credentials = creds)
        service_list.append(service)
    
    ytAnalytics, ytData = service_list
    
    # test ytData
    req = ytData.channels().list(
        part = 'id,snippet',
        mine=True)
    res = req.execute()
    print(res)
    for channel in res['items']:
        print('Channel:',channel['snippet']['title'])
    

    【讨论】:

      猜你喜欢
      • 2021-04-19
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多