【发布时间】:2018-08-07 13:42:18
【问题描述】:
几个月来我一直在努力解决这个问题,但情况越来越糟。
我在 Google Apps 脚本中有一个简单的程序,它从工作表中获取电子邮件地址并将它们放入适当的 Google 通讯录组中。直到一个月前它停止工作时,它都可以正常工作。
请帮帮我:
1) 了解我是否真的需要使用 API 来完成我正在做的事情。
2) 在下面检查我的工作是否有可能遗漏的内容。
原来的问题:
当我运行此代码时,我收到错误消息:“您无权执行该操作。”
function testAddEmail(){
ContactsApp.createContact(null, null, "tomfoolery@validemailaddress.com");
};
为了解决这个问题,我已经: - 确保在我的 Cloud Console 中启用了联系人 API。 - 多次禁用并重新启用它。 - 使用程序创建了一个新文件并启用了新的联系人 API。 -为新程序创建凭据(在此之后它工作了一天)。 - 联系 Google Cloud 支持和 G Suite 支持以确保设置正确(他们不支持脚本或联系人。)
问题 2:
我尝试添加 Oauth 2 代码,但现在它给了我错误“您无权调用 getActiveSpreadsheet。”
他们的问题在我下面的 Oauth 2 代码中吗? (我删除了我的个人信息。)
function getContactsService() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store.
return OAuth2.createService('contacts')
// Set the endpoint URLs, which are the same for all Google services.
.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
// Set the client ID and secret, from the Google Developers Console.
.setClientId('blablabla.apps.googleusercontent.com')
.setClientSecret('blablabla')
// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
// Set the scopes to request (space-separated for Google services).
.setScope('https://www.google.com/m8/feeds')
.setScope('https://www.googleapis.com/auth/spreadsheets')
// Below are Google-specific OAuth2 parameters.
// Sets the login hint, which will prevent the account chooser screen
// from being shown to users logged in with multiple accounts.
.setParam('login_hint', Session.getActiveUser().getEmail())
// Requests offline access.
.setParam('access_type', 'offline')
// Forces the approval prompt every time. This is useful for testing,
// but not desirable in a production application.
.setParam('approval_prompt', 'force');
}
【问题讨论】:
-
检查您的 Apps 脚本清单文件 - 您是否在那里设置了范围?如果是这样,它们会覆盖任何自动范围检测,并按原样使用。如果清单文件中没有任何范围,请检查 File->Project Properties->Scopes
-
"oauthScopes": [ "googleapis.com/auth/script.container.ui", "googleapis.com/auth/spreadsheets", "google.com/m8/feeds" ]
-
以上就是我现在所拥有的。它授权就好了,但不让我完成工作。
标签: google-apps-script google-oauth google-contacts-api