如何以编程方式将注释发布到Google阅读器?

时间:2011-06-04 21:40:00

标签: python google-reader

我使用Google阅读器备注作为存储书签和小片段信息的地方。我想写一个小脚本让我从命令行发布笔记(我更喜欢Python,但是会接受使用任何语言的答案)。

This project似乎是一个很好的起点和地点。一些更新的信息here。过程似乎是:

  1. https://www.google.com/accounts/ClientLogin?service=reader&Email= {0}获取SID(会话ID)& Passwd = {1}
  2. http://www.google.com/reader/api/0/token
  3. 获取临时令牌
  4. 使用正确的字段值对http://www.google.com/reader/api/0/item/edit发布POST
  5. 所以...上面的第2步总是对我失败(得到403禁止)并尝试Martin Doms C#代码有同样的问题。看起来Google不再使用此方法进行身份验证。

    更新...... This comment让我开始行动起来。我现在可以登录并获取令牌。现在我只需要弄清楚如何发布便笺。我的代码如下:

    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 个答案:

答案 0 :(得分:2)

如果您从浏览器添加Google阅读器备忘,则可以使用Firebug查看提交的内容。

它发布的网址是:http://www.google.co.uk/reader/api/0/item/edit

似乎唯一需要的参数是'T'(对于步骤2中的令牌检索)和'snippet',即发布的备注。

基于此,我做了以下适用于我的工作(注意导入urllib以及编码帖子正文):

# 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'

您可以设置其他一些参数,例如ck,linkify,share等,但它们都记录在网站上。

我从脚本的命令行参数中读取注释作为读者的练习。

相关问题