Google apps script 将重定向URI添加到自动生成的Google OAuth 2.0客户端ID
我正在尝试制作一个与YouTube数据和分析API集成的Google表单。然而,在尝试实现这一点时,我遇到了一个已知的问题,即允许品牌YouTube帐户/频道通过Google应用程序进行身份验证,这在这里进行了解释 为了解决这个问题,文档中提到了这个链接中的说明,我现在正试图自己实现这个链接 根据指示,我有:Google apps script 将重定向URI添加到自动生成的Google OAuth 2.0客户端ID,google-apps-script,oauth-2.0,google-api,youtube-api,Google Apps Script,Oauth 2.0,Google Api,Youtube Api,我正在尝试制作一个与YouTube数据和分析API集成的Google表单。然而,在尝试实现这一点时,我遇到了一个已知的问题,即允许品牌YouTube帐户/频道通过Google应用程序进行身份验证,这在这里进行了解释 为了解决这个问题,文档中提到了这个链接中的说明,我现在正试图自己实现这个链接 根据指示,我有: 将必要的库导入到脚本中 添加了必要的GoogleApps脚本代码,位于底部 在Google云控制台中创建了我自己的OAuth 2.0客户端ID凭据 然而,在第一个链接中,还添加了一条评论,
我希望这是清楚的,但如果不是这样,我可以回答任何进一步的问题。或者,如果有更好的方法可以通过API验证品牌YouTube帐户,请告诉我。最后,我不得不在控制台中创建新凭据。一旦我完成了这项工作并将其与脚本绑定在一起,它就会像预期的那样工作。我无法理解如何将重定向URI添加到自动生成的凭据中。我为此道歉。我可以问一下吗?是的,所以当我将我的谷歌应用程序脚本与我的谷歌云平台项目关联时,我是说它会自动生成一个OAuth 2.0客户端ID(上图中称为应用程序脚本的ID)。然而,有一个黄色感叹号告诉我不能编辑这些凭证,这意味着我不能添加任何重定向URI。现在明白了吗?谢谢你的回答。从你的回答中,我能理解这件事。在这种情况下,您可以添加新凭证并再次测试它吗?非常感谢。我又试了一次,现在似乎奏效了@塔奈克,这有什么理由现在能起作用吗?谢谢你的回复。当您添加新凭证时,我认为这可以用于您的情况。我认为默认凭证用于使用Google Apps脚本进行授权。这样,重定向URL就无法更改。我也遇到了这个问题,我认为目前不可能在通过API自动生成ClientID的同时添加重定向URI,因为唯一可编辑的字段是“displayName”、“name”和“secret”(参考:)。
/**
* 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;
}