将重定向 URI 添加到自动生成的 Google OAuth 2.0 客户端 ID

Add Redirect URI To Automatically Generated Google OAuth 2.0 Client ID

我正在尝试制作一个与 YouTube 数据和分析 API 集成的 Google sheet。但是,在尝试实现这一点时,我遇到了一个已知问题,即允许品牌 YouTube accounts/channels 使用 Google 应用程序进行身份验证,这在此处进行了说明。 https://issuetracker.google.com/issues/36764531

为了解决这个问题,文档提到了这个 link 中的说明,我现在正在尝试自己实施 https://mashe.hawksey.info/2017/09/identity-crisis-using-the-youtube-api-with-google-apps-script-and-scheduling-live-broadcasts-from-google-sheets/

根据说明,我有:

  1. 将必要的库导入脚本
  2. 在底部添加了必要的 Google Apps 脚本代码
  3. 在 Google 云控制台
  4. 中创建了我自己的 OAuth 2.0 客户端 ID 凭据

然而,在第一个 link 中,还添加了一条评论,说明虽然说明仍然有效,但脚本项目现在必须与云项目相关联,所以我就是这样做的。作为其中的一部分,它创建了自己的 OAuth 2.0 客户端 ID,我相信它现在正在使用而不是我自己生成的凭据。我在下面添加了一张图片来说明我的意思。然后我也无法编辑这些新凭据,这意味着我无法添加任何重定向 URI。

有没有办法将重定向 URI 添加到自动生成的凭据中?现在的问题是,如果我然后 运行 按照说明从脚本中设置函数,当我尝试打开它时 link 它给了我,然后我得到以下消息

错误 400:redirect_uri_mismatch

请求中的重定向URI,https://script.google.com/macros/d/12u2laknmO_9-zgxBbAX6wG9gJDUOvgJmYm5UquJsamShus9s5McrGBar/usercallback, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/${your_client_id}?project=${your_project_number}

/**
 * Authorizes and makes a request to the YouTube Data API.
 */
function setup() {
  var service = getYouTubeService();
  YouTube.setTokenService(function(){ return service.getAccessToken(); });
  if (service.hasAccess()) {
    var result = YouTube.channelsList("snippet", {mine:true});
    Logger.log(JSON.stringify(result, null, 2));
    throw "Open View > Logs to see result";
  } else {
    var authorizationUrl = service.getAuthorizationUrl();
    Logger.log('Open the following URL and re-run the script: %s',
        authorizationUrl);
    throw "Open View > Logs to get authentication url";
  }
}
 
 
/**
 * Configures the service.
 */
function getYouTubeService() {
  return OAuth2.createService('YouTube')
      // Set the endpoint URLs.
      .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
      .setTokenUrl('https://accounts.google.com/o/oauth2/token')
 
      // Set the client ID and secret.
      .setClientId(getStaticScriptProperty_('client_id'))
      .setClientSecret(getStaticScriptProperty_('client_secret'))
 
      // Set the name of the callback function that should be invoked to complete
      // the OAuth flow.
      .setCallbackFunction('authCallback')
 
      // Set the property store where authorized tokens should be persisted
      // you might want to switch to Script Properties if sharing access
      .setPropertyStore(PropertiesService.getUserProperties())
 
      // 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"])
      .setParam('access_type', 'offline');
}
 
/**
 * Handles the OAuth callback.
 */
function authCallback(request) {
  var service = getYouTubeService();
  var authorized = service.handleCallback(request);
  if (authorized) {
    return HtmlService.createHtmlOutput('Success!');
  } else {
    return HtmlService.createHtmlOutput('Denied');
  }
}
 
/**
 * Logs the redirect URI to register in the Google Developers Console.
 */
function logRedirectUri() {
  var service = getYouTubeService();
  Logger.log(service.getRedirectUri());
  throw "Open View > Logs to get redirect url";
}
 
/**
 * Reset the authorization state, so that it can be re-tested.
 */
function reset() {
  var service = getYouTubeService();
  service.reset();
}
 
/**
 * Gets a static script property, using long term caching.
 * @param {string} key The property key.
 * @returns {string} The property value.
 */
function getStaticScriptProperty_(key) {
  var value = CacheService.getScriptCache().get(key);
  if (!value) {
    value = PropertiesService.getScriptProperties().getProperty(key);
    CacheService.getScriptCache().put(key, value, 21600);
  }
  return value;
}

我希望这是清楚的,但如果没有,我可以回答任何进一步的问题。或者,如果有更好的方法可以使用 API 验证品牌 YouTube 帐户,请告诉我。

最后我不得不在控制台中创建新的凭据。完成此操作并将其与脚本绑定后,它似乎按预期工作。