我一直在为谷歌阅读器的客户工作。一切正常,但我无法编辑条目以添加标签,如“已加星标”和“已阅读”。 code.google.com/p/pyrfeed/wiki/GoogleReaderAPI和www.niallkennedy.com/blog/2005/12/google-reader-api.html上的说明似乎已过时。更奇怪的是,我一直在检查谷歌本身使用的POST数据,并试图完全复制它,但我仍然无法让它工作。我最接近的是,例如,http://www.google.com/reader/api/0/edit-tag有POST数据a = / user / - / state / com.google / starred& async = true& s = [feed]& i = [item] & T公司= [令牌]
这似乎正是谷歌本身所做的,但我总是回到“无效的流名称”。有什么建议吗?
答案 0 :(得分:3)
我没有给你一个明确的答案,但我在使用API api / 0 / edit-tag时遇到了一些麻烦,并设法让它们正常工作。
我已经在使用API的其他部分而没有任何问题(api / 0 / stream / items / ids,api / 0 / unread-count),但是这个部分并没有那么容易。
过了一段时间,我开始检查通过他们的网络前端发送到谷歌阅读器的请求(使用Chrome开发工具),并做了一个硬编码的例子(你可以使用这个代码,你只需要更改ID和流对于你自己 - 只要小心他们拥有所有需要的前缀:feed / for stream,以及tag:google.com,2005:reader / item / for id)。
String authToken = getGoogleAuthKey();
// I use Jsoup for the requests, but you can use anything you
// like - for jsoup you usually just need to include a jar
// into your java project
Document doc = Jsoup.connect("http://www.google.com/reader/api/0/edit-tag")
.header("Authorization", _AUTHPARAMS + authToken)
.data(
// you don't need the userid, the '-' will suffice
"a", "user/-/state/com.google/read",
"async", "true",
"s", "feed/http://www.gizmodo.com/index.xml",
"i", "tag:google.com,2005:reader/item/1a68fb395bcb6947",
"T", "//wF1kyvFPIe6JiyITNnMWdA"
)
// I also send my API key, but I don't think this is mandatory
.userAgent("[YOUR_APP_ID_GOES_HERE].apps.googleusercontent.com")
.timeout(10000)
// don't forget the post! (using get() will not work)
.**post()**;
这是我将流中的特定项标记为read的最终代码(translateToItemAtomId方法用于将api / 0 / stream / items / ids返回的长整数id转换为此接受的atom xml id API):
String authToken = getGoogleAuthKey();
Document doc = Jsoup.connect("http://www.google.com/reader/api/0/edit-tag")
.header("Authorization", _AUTHPARAMS + authToken)
.data(
"a", "user/-/state/com.google/read",
"async", "true",
"s", stream,
"i", translateToItemAtomId(itemId),
"T", getGoogleToken(authToken)
)
.userAgent("[YOUR_APP_ID_GOES_HERE].apps.googleusercontent.com")
.timeout(10000).post();
您可能需要的一些额外代码(基于http://www.chrisdadswell.co.uk/android-coding-example-authenticating-clientlogin-google-reader-api/):
private static final String _AUTHPARAMS = "GoogleLogin auth=";
private static final String _GOOGLE_LOGIN_URL = "https://www.google.com/accounts/ClientLogin";
private static final String _READER_BASE_URL = "http://www.google.com/reader/";
private static final String _API_URL = _READER_BASE_URL + "api/0/";
private static final String _TOKEN_URL = _API_URL + "token";
private static final String _USER_INFO_URL = _API_URL + "user-info";
private static final String _USER_LABEL = "user/-/label/";
private static final String _TAG_LIST_URL = _API_URL + "tag/list";
private static final String _EDIT_TAG_URL = _API_URL + "tag/edit";
private static final String _RENAME_TAG_URL = _API_URL + "rename-tag";
private static final String _DISABLE_TAG_URL = _API_URL + "disable-tag";
private static final String _SUBSCRIPTION_URL = _API_URL
+ "subscription/edit";
private static final String _SUBSCRIPTION_LIST_URL = _API_URL
+ "subscription/list";
public static String getGoogleAuthKey() throws IOException {
String _USERNAME = "USER_EMAIL@gmail.com";
String _PASSWORD = "USER_PASSWORD";
Document doc = Jsoup
.connect(_GOOGLE_LOGIN_URL)
.data("accountType", "GOOGLE", "Email", _USERNAME, "Passwd",
_PASSWORD, "service", "reader", "source",
"[YOUR_APP_ID_GOES_HERE].apps.googleusercontent.com")
.userAgent("[YOUR_APP_ID_GOES_HERE].apps.googleusercontent.com")
.timeout(4000).post();
// RETRIEVES THE RESPONSE TEXT inc SID and AUTH. We only want the AUTH
// key.
String _AUTHKEY = doc
.body()
.text()
.substring(doc.body().text().indexOf("Auth="),
doc.body().text().length());
_AUTHKEY = _AUTHKEY.replace("Auth=", "");
return _AUTHKEY;
}
// generates a token for edition, needed for edit-tag
public static String getGoogleToken(String authToken) throws IOException {
Document doc = Jsoup.connect(_TOKEN_URL)
.header("Authorization", _AUTHPARAMS + getGoogleAuthKey())
.userAgent("[YOUR_APP_ID_GOES_HERE].apps.googleusercontent.com")
.timeout(10000).get();
// RETRIEVES THE RESPONSE TOKEN
String _TOKEN = doc.body().text();
return _TOKEN;
}
希望这有帮助!