2

リクエストでプロトコル仕様を使用して、Google スプレッドシートの基本的な機能を実装しようとしています。

私がこれを行っている理由は、これが Android 用であり、gdata-java ライブラリが実際には機能せず、アルファ android ライブラリが実際にカットされていないためです。私は認証を実装し、リストを取得し、削除することができましたが、行を編集または更新するために、私はそれについて本当に頭を悩ませることができません。

たとえば、セルの内容を変更したい場合 は、ここにプロトコルの仕様があります

私の理解では、ある種のatomまたはxml形式のリクエストを編集URLに送信する必要があります。私はそのようなタイプのリクエストを送信したことはありません。

これは 、それがどのように行われるべきかについての手がかりを与えます(フォーラムでの未回答の質問でもあります)が、実際にはそれを管理しませんでした。

これが必要な場合の認証関数です。コードを試してみたい場合は、再度実装する必要はありません。

/**
 * Logs in to the Google service using the ClientLogin HttpRequest API and returns an authentification token
 */
 private String getAuthToken() {


HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(CLIENT_LOGIN_ADDRESS);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
nameValuePairs.add(new BasicNameValuePair("Email", "username@gmail.com"));
nameValuePairs.add(new BasicNameValuePair("Passwd", "password"));
nameValuePairs.add(new BasicNameValuePair("service", "wise"));
nameValuePairs.add(new BasicNameValuePair("source", SERVICE_NAME));

try {
  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
catch (UnsupportedEncodingException e) {
  //Log.e("ERROR", "UnsupportedEncodingException");
}

//Log.v("TEST", "Executing request " + httppost.getURI());

ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;

try {
  responseBody = httpclient.execute(httppost, responseHandler);
}
catch (ClientProtocolException e) {
  //Log.e("ERROR", "ClientProtocolException");
}
catch (IOException e) {
  //Log.e("ERROR", "IOException");
}

//Log.v("TEST", "response:" + responseBody);

String[] vals = responseBody.split("\n")[2].split("=");
String auth_string = vals[1];
//Log.v("TEST", "auth_token:" + vals[1]);

return auth_string;
}

更新機能として私が試しているのは次のとおりです。注、編集 URL は私のドキュメント用であり、試してみると機能しません。

/**
 * Ignore this i use it for testing
 */
 public void justTesting() {

String url = "http://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1/1q0cdh";

HttpClient httpclient = new DefaultHttpClient();

HttpPut httpput = new HttpPut(url);
httpput.addHeader(new BasicHeader("Authorization", "GoogleLogin auth=" + getAuthToken()));
httpput.addHeader(new BasicHeader("GData-Version", "2.0"));

HttpEntity he = null;

String messageBody = "<entry>"+
                     "<id>https://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1</id>"+
                     "<link rel=\"edit\" type=\"application/atom+xml\""+
                     "  href=\"https://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1\"/>"+
                     "<gs:cell row=\"2\" col=\"2\" inputValue=\"300\"/>"+
                      "</entry>";


try {
  he = new StringEntity(messageBody);
}
catch (UnsupportedEncodingException e) {
  //Log.e("ERROR", "UnsupportedEncodingException");
}

httpput.setEntity(he);


try {

  HttpResponse hr = httpclient.execute(httpput);
  //Log.d("DEBUG", "sl : " + hr.getStatusLine());
}
catch (ClientProtocolException e) {
  //Log.e("ERROR", "ClientProtocolException");
}
catch (IOException e) {
  //Log.e("ERROR", "IOException");
}

//Log.v("TEST", "executed");

}

これにより、現在 400 Bad リクエストが返されます。また、Apache HttpClient を使用してこれを達成しようとしています。

これがどのように達成されるか、\実装されるか、\どのリクエストをどのように送信する必要があるかについて、誰かが考えを持っていますか?

ありがとう、あなたの助けは大歓迎です。


私はいくつかの進歩を遂げ、これを書きました:

public void justTesting() {

String url = "https://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1";

HttpClient httpclient = new DefaultHttpClient();

HttpPut httpput = new HttpPut(url);
httpput.addHeader(new BasicHeader("Authorization", "GoogleLogin auth=" + getAuthToken()));
httpput.addHeader(new BasicHeader("GData-Version", "3.0"));

HttpEntity he = null;

String messageBody = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'> <id>http://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1 </id> <gs:cell row='2' col='1' inputValue='mouuuuuuuuusee'/> </entry>";
  //RequestEntity requestEntity = new StringRequestEntity(xml.toString(), "application/atom+xml", "UTF-8");


try {
  he = new StringEntity(messageBody);
}
catch (UnsupportedEncodingException e) {
  Log.e("ERROR", "UnsupportedEncodingException");
}

httpput.addHeader("Content-Type", "application/atom+xml");
httpput.setEntity(he);


try {
  HttpResponse hr = httpclient.execute(httpput);
  Log.d("DEBUG", "sl : " + hr.getStatusLine());
}
catch (ClientProtocolException e) {
  Log.e("ERROR", "ClientProtocolException");
}
catch (IOException e) {
  Log.e("ERROR", "IOException");
}

Log.v("TEST", "executed");

}

今、それは私に 403 Forbidden を与えます。理由はありますか?

4

1 に答える 1

1

ねえ、私はそれを理解しました、私はこれを見逃していました:

httpput.addHeader(new BasicHeader("If-Match", "*"));

関数は次のようになります。

  public void justTesting() {

String url = "http://spreadsheets.google.com/feeds/cells/t9VU1IwRrmG3h-nhI_J2fzg/od6/private/full/R2C1";

HttpClient httpclient = new DefaultHttpClient();

HttpPut httpput = new HttpPut(url);
httpput.addHeader(new BasicHeader("Authorization", "GoogleLogin auth=" + getAuthToken()));
httpput.addHeader(new BasicHeader("GData-Version", "2.0"));
httpput.addHeader(new BasicHeader("If-Match", "*"));

HttpEntity he = null;

String messageBody = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'> <id>http://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1</id> <link rel='edit' type='application/atom+xml' href='http://spreadsheets.google.com/feeds/cells/t9VU1IwRrmG3h-nhI_J2fzg/od6/private/full/R2C1/1en5'/> <gs:cell row='2' col='1' inputValue='mouuuuuuuuusee' /> </entry>";

try {
  he = new StringEntity(messageBody);
}
catch (UnsupportedEncodingException e) {
  Log.e("ERROR", "UnsupportedEncodingException");
}

httpput.addHeader("Content-Type", "application/atom+xml");
httpput.setEntity(he);


try {
  HttpResponse hr = httpclient.execute(httpput);
  Log.d("DEBUG", "sl : " + hr.getStatusLine());
}
catch (ClientProtocolException e) {
  Log.e("ERROR", "ClientProtocolException");
}
catch (IOException e) {
  Log.e("ERROR", "IOException");
}

Log.v("TEST", "executed");

}

于 2010-07-16T07:16:25.687 に答える