2

Googleドライブに新しいフォルダーを作成したいのですが、私のコードは次のとおりです:

OAuthRequest request = new OAuthRequest(Verb.POST, "https://www.googleapis.com/drive/v2/files");

request.addBodyParameter("title", "Folder");
request.addBodyParameter("mimeType", "application/vnd.google-apps.folder");

service.signRequest(accessToken, request);

Response response = request.send();

この方法で 401 Unauthorized を取得しています。addBodyParameter 行を取り出すと動作しますが、無題のファイルが作成されます。

何が問題なのかわかりません。コードの行がいくつか欠けている可能性があります。この問題を解決する方法を知っている人はいますか? 前もって感謝します。

私はそれを修正しました:

OAuthRequest request = new OAuthRequest(Verb.POST, "https://www.googleapis.com/drive/v2/files");

JSONObject res = new JSONObject();
res.put("title", "Folder");
res.put("mimeType", "application/vnd.google-apps.folder");

request.addPayload(res.toJSONString());
request.addHeader("Content-Length", ""+res.size());
request.addHeader("Content-Type", "application/json");

service.signRequest(accessToken, request);

Response response = request.send();

タイトルと mimeType を JSONObject に入れ、ペイロードとしてリクエストに追加し、リクエストのヘッダーに json の content-length と content-type を追加する必要がありました。今では正常に動作しています。

後で他の誰かに役立つことを願っています。

4

0 に答える 0