如何使用Google Apps Script Execution API + Java创建Google表单?

时间:2016-04-29 20:10:52

标签: java google-apps-script google-api

我尝试了一个简单的Java命令行应用程序,该应用程序向Google Apps脚本执行API发出请求。这样的事情:https://developers.google.com/apps-script/guides/rest/quickstart/java#prerequisites。但它不起作用。

Google Apps脚本编辑器:

function createForm() {

  var form = FormApp.create('New Form');

}

Java错误:

Credentials saved to /home/palmaslab/.credentials/script-java-quickstart.json
com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
        at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
        at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
        at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
        at org.gradle.AppsScriptApiQuickstart.main(AppsScriptApiQuickstart.java:188)

2 个答案:

答案 0 :(得分:1)

API在调用API时返回HTTP 401响应。这些错误可能表明以下任何一个:

令牌到期,令牌撤销,令牌未获得所需范围的授权,请求未经OAuth 2.0 protocol正确授权。您还可以查看Forms Service以获取有关表单服务的更多信息

以下是401的相关问题:未经授权:Google apps script and execution API: latest version of script throws error

答案 1 :(得分:0)

似乎您必须先授权范围。 也许它可以帮助你: Manual authorization scopes for Sheets, Docs, and Forms