我可以從我的應用程序成功訪問Google雲端硬盤和電子表格功能。如何從Android訪問內置Google Apps腳本的「Web App」?
所以我有一個授權的com.google.api.client.auth.oauth2.Credential實例。
現在我希望執行部署爲'Web App'的Google Apps腳本。這也需要驗證才能運行。如果我點擊終端並進行身份驗證,該腳本將在瀏覽器中運行。
下面是一些僞代碼:
String url = "https://script.google.com/a/macros/mycompany.com/s/xxxx/dev";
GenericUrl webAppEndPoint = new GenericUrl(url);
final HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(currentCredential);
// Do POST for service
String requestBody = URLEncoder.encode("{\"name\":\"John Smith\",\"company\":\"Virginia Company\",\"pdf\":\""+getPdfBase64()+"\"}", "UTF-8");
HttpRequest postRequest =requestFactory.buildPostRequest(new GenericUrl(url), ByteArrayContent.fromString(null, requestBody));
postRequest.getHeaders().setAccept("application/json");
postRequest.setFollowRedirects(true);
postRequest.setLoggingEnabled(true);
HttpResponse postResponse = postRequest.execute();
如果我運行代碼,我得到了以下錯誤:com.google.api.client.http.HttpResponseException:HttpResponseException 405不允許的方法
更新:所以 - 最初我是發佈到錯誤的URL(我從瀏覽器中複製了重定向的URL而不是腳本URL)
使用上述代碼,POST現在已成功(包含身份驗證),但它仍然沒有'處理GET重定向ct提交後。我現在可以使用它,但是能夠從服務器獲得響應是很好的。
我認爲com.google.api.client.http.HttpRequest不能正確處理經過驗證的POST重定向。
所以這似乎揭示了一些問題 - https://code.google.com/p/google-apps-script-issues/issues/detail?id = 2707 - POST重定向併成爲GET – dompardoe