谷歌小号preadsheet API更新\编辑与协议 [英] Google Spreadsheet API update \ edit with protocol
问题描述
我想实现的谷歌小号preadsheets一些基本的功能, 使用协议规范与要求。
I'm trying to implement some basic functionality for Google Spreadsheets, using the protocol specification with requests.
的原因,我这样做,因为它是为Android和gdata的-Java库并没有真正的工作和阿尔法机器人并没有真正削减它。 我设法实现认证,并获得名单,并删除,但编辑\更新行我真的不能环绕它我的脑海里。
The reason i'm doing this because it is for Android, and gdata-java library doesn't really work and the alpha android one doesn't really cut it. I managed to implement authentication, and get for lists, and delete, but for editing \ updating a row i can't really wrap my mind around it.
例如我想改变一个单元格的内容 <一href="http://$c$c.google.com/apis/s$p$padsheets/data/2.0/developers_guide_protocol.html#UpdatingCells"相对=nofollow>此处是规范的协议
For example i want to change the contents of a cell here is the specification for the protocol
这是我对它的理解,我要发送某种原子或XML格式的请求来编辑URL。我从来没有发这种类型的请求。
From my understanding of it, i have to send some sort of atom or xml formatted request to the edit URL. I never sent such type of requests.
我还发现<一href="http://www.devcomments.com/I-need-to-insert-update-delete-events-in-google-calendar-using-to112717.htm"相对=nofollow>这个,这给它应该怎么做(同样在论坛上一个没有答案的问题),但一个线索并没有真正做的。
I also found this, which gives a clue on how it should be done (also an unanswered question on a forum), but didn't really managed it.
下面是我的身份验证功能,如果你需要它,这样你就不会再执行它,如果你想尝试和code吧。
Here is my authentication function if you need it, so you don't implement it again, if you want to try and code it.
/**
* 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;
}
下面是我想要的更新功能,请注意,编辑网址是为我的文件将无法正常工作,如果你尝试它,它只是一个IEA对我到目前为止有:
Here is what i'm trying as the update function, note, edit URL's are for my document won't work if you try it, it's just for an iea on what i have so far :
/**
* 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错误请求。 另外我想实现这个使用Apache的HttpClient。
This currently gives a 400 Bad request. Also I'm trying to achieve this using Apache HttpClient.
有没有人对如何可能实现任何想法\执行\怎么样的要求,我应该送?
Does anyone have any idea on how this might be achieved \ implemented \ how what request i should send?
谢谢你的帮助会大大AP preciated。
Thanks, your help will be greatly appreciated.
我取得了一些进展,并写了:
I made some progress and wrote this :
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禁止。知道为什么吗?
Now it gives me a 403 Forbidden. Any idea why?
推荐答案
嘿,我想通了,我错过了这一点:
Hey, I figured it out, i was missing this :
httpput.addHeader(new BasicHeader("If-Match", "*"));
和现在的功能是这样的:
and now the function looks like this :
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");
}
这篇关于谷歌小号preadsheet API更新\编辑与协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!