【发布时间】:2016-04-07 18:31:56
【问题描述】:
我们已经开发了一个应用程序,它将使用 OAuth 1.0 从 GAS 脚本与 GAE 应用程序交互,但我们无法使用 OAuth 2.0 实现它,我们从 OAuth 获得访问失败。
getservice.hasAccess() 总是返回 false。我对必须使用哪个 URL 进行身份验证过程感到非常困惑。
我已附上 OAuth 1 和 OAuth 2 的源代码以供参考
Oauth 1.0
=========
envVars.url = myapp.appspot.com;
var oAuthConfig = UrlFetchApp.addOAuthService(envVars.serviceName);
oAuthConfig.setRequestTokenUrl(envVars.url + '/_ah/OAuthGetRequestToken');
oAuthConfig.setAuthorizationUrl(envVars.url + '/_ah/OAuthAuthorizeToken');
oAuthConfig.setAccessTokenUrl(envVars.url + '/_ah/OAuthGetAccessToken');
var requestData = {
"oAuthServiceName": envVars.serviceName,
"oAuthUseToken": "always"
};
requestData.muteHttpExceptions = true;
Oauth 2.0
=========
envVars.url = myapp.appspot.com
getservice = getService();
if (getservice.hasAccess()) {
Logger.log("All OK");
return;
}
else {
var authorizationUrl = getservice.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s',
authorizationUrl);
showURL(authorizationUrl);
}
function getService()
{
return OAuth2.createService(envVars.serviceName)
// Set the endpoint URLs.
.setAuthorizationBaseUrl(envVars.url + '/_ah/OAuthAuthorizeToken')
.setTokenUrl(envVars.url + '/_ah/OAuthGetAccessToken')
// Set the client ID and secret.
.setClientId(CLIENT_ID)
.setClientSecret(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.
.setPropertyStore(PropertiesService.getUserProperties());
}
【问题讨论】:
标签: google-app-engine google-apps-script oauth-2.0 google-oauth