簡體   English   中英

如何從Android訪問內置於Google Apps腳本中的“網絡應用”?

[英]How can I access a 'Web App' built in Google Apps Script from Android?

我可以從我的應用程序成功訪問Google雲端硬盤和電子表格功能。

因此,我有com.google.api.client.auth.oauth2.Credential的授權實例。

現在,我希望執行一個部署為“ Web應用程序”的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重定向。 我現在可以使用它,但是能夠從服務器獲得響應將是很好的。

我認為com.google.api.client.http.HttpRequest無法正確處理經過身份驗證的POST重定向。

您的偽代碼不是很有啟發性。 要真正了解發生了什么,您需要顯示實際的HTTP流量。 我應該說,雖然302重定向到指定的redict_uri是OAuth 2身份驗證流程的正常部分。

1)您無法通過身份驗證調用應用程序腳本。 您需要將其作為contentService的匿名訪問發布。 2)您也輸入了錯誤的網址。 調用服務URL,而不是您在瀏覽器中獲得的重定向URL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM