アプリケーションから Google ドライブとスプレッドシートの機能に正常にアクセスできます。
したがって、com.google.api.client.auth.oauth2.Credential の承認済みインスタンスがあります。
ここで、「Web アプリ」としてデプロイされた Google Apps Script を実行したいと考えています。これを実行するには認証も必要です。このスクリプトは、エンドポイントに到達して認証されると、ブラウザーで実行されます。
ここにいくつかの疑似コードがあります:
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 Method Not Allowed
更新:だから-もともと私は間違ったURLに投稿していました(スクリプトURLの代わりにブラウザからリダイレクトされたURLをコピーしました)
上記のコードを使用して POST が成功しました (認証が含まれています) が、送信後に GET リダイレクトをまだ処理していません。これで作業できますが、サーバーから応答を取得できるとよいでしょう。
com.google.api.client.http.HttpRequest は、認証された POST リダイレクトを適切に処理していないと思います。