YouTube Apps Script API only runnable by accounts without a YouTube channel?

无人久伴 提交于 2019-12-02 07:56:21

There is a related issue ticket which Google have marked as 'Wont't fix' which describes your problem:

If you are trying to use the YouTube API to get data about a YouTube Channel that you manage using the Google+ association system, it will get you stuck in an infinite loop. Google+ pages can “own” a YouTube channel, and the authorization will not work when trying to use one of those channels

Google do have a suggested workaround:

use the OAuth2 library to authorize access to the G+ page, and then use UrlFetchApp instead of the YouTube Advanced service to make requests to the API

As this is a problem I've also encountered I've published a detailed tutorial that uses the YouTube Data API. As the code you require also needs access via to the YouTube Analytics API there is some additional setup required detailed below and referencing the part in the tutorial:

  1. Add a YouTube Analytics library with id 1MWD64g7dq_ZhlN8HU_O6BRu5xNwywhp8V76utKowZEtcirEgO3t_JFFL (Part 1.4)
  2. Add additional scopes to getYouTubeService() for yt-analytics-monetary.readonly and yt-analytics.readonly (Part 2.2)

    // Set the scope and additional Google-specific parameters.
    .setScope(["https://www.googleapis.com/auth/youtube",
              "https://www.googleapis.com/auth/youtube.force-ssl",
              "https://www.googleapis.com/auth/youtube.readonly",
              "https://www.googleapis.com/auth/youtubepartner",
              "https://www.googleapis.com/auth/youtubepartner-channel-audit",
              "https://www.googleapis.com/auth/yt-analytics-monetary.readonly",
              "https://www.googleapis.com/auth/yt-analytics.readonly"])
    
  3. Enabling the YouTube Analytics API as well as the YouTube Data API (Part 3.4)

There is also some minor modification in your code sample as to enable the library autocomplete e.g. YouTube.Channels.list() becomes YouTube.channelsList() and YouTubeAnalytics.Reports.query() becomes YouTubeAnalytics.reportsQuery()

All these changes have been included in this example sheet. You'll still need to do all the setup of the console project outlined in the tutorial and in the auth.gs sheet (I've modified the example to run in @OnlyCurrentDoc to avoid app verification).

Note: when you run logRedirectUri() you need to authenticate with your Google Drive account and when copy the authentication url in a new browser tab select the YouTube account you want data for.

Make sure that you are using the right scope for this. To retrieve the information about a specific reporting job that has been scheduled for a channel or content owner, you must use the following authorization scopes:

  • https://www.googleapis.com/auth/yt-analytics.readonly
    • View YouTube Analytics reports for your YouTube content. This scope provides access to user activity metrics, like view counts and rating counts.
  • https://www.googleapis.com/auth/yt-analytics-monetary.readonly
    • View YouTube Analytics monetary reports for your YouTube content. This scope provides access to user activity metrics and to estimated revenue and ad performance metrics.

Check this link for more information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!