问题
I use my Google Reader notes as a place to store bookmarks and small snippets of information. I would like to write a small script to let me post notes from the command line (I prefer Python,but an answer using any language will be accepted).
This project seemed to be a be a good place to start & some more up-to-date information here. The process appears to be:
- Get a SID (session ID) from https://www.google.com/accounts/ClientLogin?service=reader&Email={0}&Passwd={1}
- Get a temporary token from http://www.google.com/reader/api/0/token
- Make a POST to http://www.google.com/reader/api/0/item/edit with the correct field values
So... step 2 above always fails for me (get a 403 forbidden) and trying Martin Doms C# code has the same issue. It looks like Google no longer use this method for authentication.
Update... This comment got me up and running. I can now log in and get a token. Now I just need to figure out how to POST the note. My code is below:
import urllib2
# Step 1: login to get session auth
email = 'myuser@gmail.com'
passwd = 'mypassword'
response = urllib2.urlopen('https://www.google.com/accounts/ClientLogin?service=reader&Email=%s&Passwd=%s' % (email,passwd))
data = response.read()
credentials = {}
for line in data.split('\n'):
fields = line.split('=')
if len(fields)==2:
credentials[fields[0]]=fields[1]
assert credentials.has_key('Auth'),'no Auth in response'
# step 2: get a token
req = urllib2.Request('http://www.google.com/reader/api/0/token')
req.add_header('Authorization', 'GoogleLogin auth=%s' % credentials['Auth'])
response = urllib2.urlopen(req)
# step 3: now POST the details of note
# TBD...
回答1:
Using Firebug you can see what gets submitted if you add a Google Reader note from a browser.
The url it posts to is : http://www.google.co.uk/reader/api/0/item/edit.
It seems that the only required parameters are 'T' (for the token retrieve at step 2) and 'snippet' which is the note being posted.
Based on that I did the following which works for me (note import urllib as well encode the post body):
# step 3: now POST the details of note
import urllib
token = response.read()
add_note_url = "http://www.google.co.uk/reader/api/0/item/edit"
data = {'snippet' : 'This is the note', 'T' : token}
encoded_data = urllib.urlencode(data)
req = urllib2.Request(add_note_url, encoded_data)
req.add_header('Authorization', 'GoogleLogin auth=%s' % credentials['Auth'])
response = urllib2.urlopen(req)
# this part is optional
if response.code == 200:
print 'Gadzooks!'
else:
print 'Curses and damnation'
There are a couple of other params that you can set e.g. ck, linkify, share etc, but they are all documented on the site.
I leave reading the note from a command line argument to the script as an exercise for the reader.
来源:https://stackoverflow.com/questions/6239560/how-can-i-can-i-programatically-post-a-note-to-google-reader