google bookmarket api
引用:
最近做了google书签同步的模块,发现google并没有公开bookmark相关的api接口,在网上也找了些资料,通过自己抓包分析,测试,总结下使用bookmark接口的心得,我是在android下测试的,应该不存在编程语言的问题。
1.登录google账户(POST)
由于google bookmark没有公布api,测试google各种认证方式并通过抓包分析,发现想要获得bookmark服务,在请求时必须传递已登录后的Cookie信息。
请求:https://www.google.com/accounts/ServiceLoginAuth
头信息:Cookie="GALX=任意字符串" (此头信息必须有,其他头信息可根据需求添加,否则无法获取到Cookie,任意字符串也很重要,请求参数和此处应一致)
参数:GALX="字符串" (头信息中定义的GALX字符串,必须一致)
Email="用户名"
Passwd="密码"
Java代码可以用DefaultHttpClient方式测试,该类提供了获取和设置Cookie方法,通过验证Cookie中是否有SID,即可验证是否正确登录获得了Cookie。
- /**
- * 登录google账户
- */
- private boolean googleLogin() {
- HttpPost post = new HttpPost("https://www.google.com/accounts/ServiceLoginAuth");
- post.addHeader("Cookie", "GALX=zidangtou");
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
- nvps.add(new BasicNameValuePair("GALX", "zidangtou"));
- nvps.add(new BasicNameValuePair("Email", "******@gmail.com"));
- nvps.add(new BasicNameValuePair("Passwd", "*****"));
- try {
- post.setEntity(new UrlEncodedFormEntity(nvps));
- HttpResponse response = mClient.execute(post);
- if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK && validateLogin()) {
- return true;
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return false;
- }
2..获取书签列表(GET)
请求:https://www.google.com/bookmarks/?output=xml&sort=title&num=1000
参数说明:output:导出的方式xml/rss
sort:排序方式,title 以标题排序,date 以建立该Bookmark日期排序
Num:书签数量
若要添加,修改或删除书签,必须要通过https://www.google.com/bookmarks/?output=rss方式,获取响应实体,从中拿到sig签名数据,才能在修改书签时成功修改。
- /**
- * 获取服务器书签数据,获取sig参数
- */
- private void getBookmarks() {
- HttpGet get = new HttpGet("https://www.google.com/bookmarks/lookup?output=rss");
- try {
- HttpResponse response = mClient.execute(get);
- if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- // Log.d("getBookmarks", EntityUtils.toString(response.getEntity()));
- mSig= getSignature(response.getEntity().getContent());
- Log.d("getSignature", "mSig = "+mSig);
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
3.添加/修改书签(POST)
请求:https://www.google.com/bookmarks/mark
或https://www.google.com/bookmarks/mark?op=edited
或https://www.google.com/bookmarks/mark?op=updated
参数:sig=签名认证(获取书签列表时以https://www.google.com/bookmarks/lookup?output=rss获取时得到)
bkmk=URL
title=标题
labels=标签(可以为多个,以逗号隔开)
annotation=注解:
若书签已存在(URL相同),则修改服务器书签,完全覆盖(label被完全替换为传上去的标签)。
- <span style="white-space: pre;"> </span>/**
- * 添加单个书签
- */
- private void addBookmark() {
- HttpPost post = new HttpPost("https://www.google.com/bookmarks/mark");
- ArrayList<BasicNameValuePair> bnvps = new ArrayList<BasicNameValuePair>();
- bnvps.add(new BasicNameValuePair("bkmk","www.ceshi.com"));
- bnvps.add(new BasicNameValuePair("title","title_ceshi"));
- bnvps.add(new BasicNameValuePair("annotation","anno_ceshi"));
- bnvps.add(new BasicNameValuePair("labels","label_ceshi"));
- try {
- post.setEntity(new UrlEncodedFormEntity(bnvps));
- mClient.execute(post);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
4.批量添加/修改书签(POST)
请求:https://www.google.com/bookmarks/mark?op=upload_selection&sig=签名字符串
参数:bm_序号="X"
url_序号=URL
ttl_序号=标题
lbl_序号=标签
参数中可添加多个书签,以序号区别,例:
bm_1=X,url_1=www.shuqian1.com,ttl_1=书签1,lbl_1=标签1
bm_2=X,url_2=www.shuqian2.com,ttl_2=书签2,lbl_2=标签2
此接口特性:若书签已存在,则修改服务器书签,但并不覆盖(若服务器书签有label,则上传的label会被加载已有label的后面)
- /**
- * 批量添加多个书签
- */
- private void batchAddBookmarks() {
- HttpPost post = new HttpPost("https://www.google.com/bookmarks/mark?op=upload_selection&sig="+mSig);
- ArrayList<BasicNameValuePair> bnvps = new ArrayList<BasicNameValuePair>();
- bnvps.add(new BasicNameValuePair("bm_1","X"));
- bnvps.add(new BasicNameValuePair("url_1","www.ceshi1.com"));
- bnvps.add(new BasicNameValuePair("ttl_1","ceshi1"));
- bnvps.add(new BasicNameValuePair("lbl_1","111"));
- bnvps.add(new BasicNameValuePair("bm_2","X"));
- bnvps.add(new BasicNameValuePair("url_2","www.ceshi2.com"));
- bnvps.add(new BasicNameValuePair("ttl_2","ceshi2"));
- bnvps.add(new BasicNameValuePair("lbl_2","222"));
- try {
- post.setEntity(new UrlEncodedFormEntity(bnvps));
- mClient.execute(post);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
5.删除书签(GET)
请求:https://www.google.com/bookmarks/mark?dlq=书签Id&sig=签名字符串
获取书签时以output=xml请求到的数据里有书签id信息。
- /**
- * 删除书签
- */
- private void deleteBookmark() {
- HttpGet get = new HttpGet("https://www.google.com/bookmarks/mark?dlq=书签id&sig="+mSig);
- try {
- mClient.execute(get);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
总结http://zidangtou.iteye.com/blog/748692
1.必须获取到cookie信息后才能正确调用bookmark接口(详见登录google账户)
2.上述接口调用时httpClient用的是同一个对象,里面有cookie信息
3.Sig签名的获取:详见获取书签列表
4.bookmark的接口必须为https类型
参考资料
1.http://www.bookmarked.mobi/bm/google_bookmarks_api/
2.http://chuiwenchiu.spaces.live.com/blog/cns!CA5D9227DF9E78E8!860.entry