python - 如何在 Python 中使用 Google Blogger API?

标签 python blogger blogspot

我正在尝试使用来自 Google API gdata 的博客功能。我按照文档尽力而为,但失败得很惨。谁能告诉我如何使用 Google Blogger API?我的代码很乱,现在我不知道了。

编辑完整的工作代码 :) :

from oauth2client.client import OAuth2WebServerFlow
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage

#flow = OAuth2WebServerFlow(client_id='', #ID
#                           client_secret='', #SECRET ID
#                           scope='https://www.googleapis.com/auth/blogger',
#                           redirect_uri='urn:ietf:wg:oauth:2.0:oob')

#auth_uri = flow.step1_get_authorize_url()
# Redirect the user to auth_uri on your platform.

# Open a file
#fo = open("foo.txt", "wb")
#fo.write( auth_uri +"\n");
#fo.close()

#credentials = flow.step2_exchange( raw_input ( ) ) 


storage = Storage('a_credentials_file')
#storage.put(credentials)

credentials = storage.get()

http = httplib2.Http()
http = credentials.authorize(http)

service = build('blogger', 'v3', http=http)

users = service.users() 

# Retrieve this user's profile information
thisuser = users.get(userId='self').execute()
print('This user\'s display name is: %s' % thisuser['displayName'])

最佳答案

当我自己尝试寻找解决方案时,我找到了 this 。然后经过一些修改,代码终于可以运行了。它成功打印了有关您博客站点的所有详细信息。

from oauth2client.client import flow_from_clientsecrets
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
import webbrowser

def get_credentials():
    scope = 'https://www.googleapis.com/auth/blogger'
    flow = flow_from_clientsecrets(
        'client_secrets.json', scope,
        redirect_uri='urn:ietf:wg:oauth:2.0:oob')
    storage = Storage('credentials.dat')
    credentials = storage.get()

    if  not credentials or credentials.invalid:
        auth_uri = flow.step1_get_authorize_url()
        webbrowser.open(auth_uri)
        auth_code = raw_input('Enter the auth code: ')
        credentials = flow.step2_exchange(auth_code)
        storage.put(credentials)
    return credentials

def get_service():
    """Returns an authorised blogger api service."""
    credentials = get_credentials()
    http = httplib2.Http()
    http = credentials.authorize(http)
    service = build('blogger', 'v3', http=http)
    return service

if __name__ == '__main__':
    served = get_service()
    blogs = served.blogs()
    blog_get_obj = blogs.get(blogId='123456789123456')
    details = blog_get_obj.execute()
    print details

打印结果如下:

{u'description': u'Look far and wide. There are worlds to conquer.',
 u'id': u'8087466742945672359',
 u'kind': u'blogger#blog',
 u'locale': {u'country': u'', u'language': u'en', u'variant': u''},
 u'name': u'The World Around us',
 u'pages': {u'selfLink': u'https://www.googleapis.com/blogger/v3/blogs/1234567897894569/pages',
            u'totalItems': 2},
 u'posts': {u'selfLink': u'https://www.googleapis.com/blogger/v3/blogs/1245678992359/posts',
            u'totalItems': 26},
 u'published': u'2015-11-02T18:47:02+05:30',
 u'selfLink': u'https://www.googleapis.com/blogger/v3/blogs/9874652945672359',
 u'updated': u'2017-06-29T19:41:00+05:30',
 u'url': u'http://www.safarnuma.com/'}

关于python - 如何在 Python 中使用 Google Blogger API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37851663/

相关文章:

image - 在博客上添加 svg 图片作为标题

html - AMP 轮播中的字幕不起作用

python - tf.reduce_sum(lastconv,axis=2)/tf.reduce_sum(tf.cast(tf.greater(lastconv, 0), tf.float32), axis=2) 用于代替均值池?

python - 在计划任务的请求上下文之外更改 Flask-Babel 语言环境

Python 文档测试 : result with multiple lines

html - 如何让页脚粘在博客底部?

python - 如何使用马拉松比赛运行多个 session ?

css - 将颜色更改为博主标题的一个元素

javascript - 需要博主的下一篇文章按钮

html - Linkwithin-如何删除?