首页 > 解决方案 > colab博主API授权

问题描述

使用这个脚本python-blogger-testting.py

import httplib2
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run_flow
from googleapiclient import discovery
CLIENT_SECRET = 'client_secret.json'
SCOPE = 'https://www.googleapis.com/auth/blogger'
STORAGE = Storage('credentials.storage')
# Start the OAuth flow to retrieve credentials
def authorize_credentials():
# Fetch credentials from storage
    credentials = STORAGE.get()
# If the credentials doesn't exist in the storage location then run the flow
    if credentials is None or credentials.invalid:
        flow = flow_from_clientsecrets(CLIENT_SECRET, scope=SCOPE)
        http = httplib2.Http()
        credentials = run_flow(flow, STORAGE, http=http)
    return credentials
credentials = authorize_credentials()
print(credentials)

def get_pages_from_jobs():
    credentials = authorize_credentials()
    http = credentials.authorize(httplib2.Http())
    discoveryUrl = ('https://www.googleapis.com/blogger/')
    service = discovery.build('blogger', 'v3', http=http, discoveryServiceUrl=discoveryUrl) 
    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'])
    page=service.pages()
##    onepage=page.get(blogId='xxxxxxxx',pageId='xxxxx').execute()
##    print("this will show specific onepage", onepage)
##    cities=['united states','India','Aus','brazil']
##    for item in cities:
##        title="title for "+item
##        payload={
##            "content": "<div dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\">\nEspeciallySports is a sports news portal aims at bringing the latest sports news for the fans that cover MMA, WWE, UFC, Cricket, Football, etc&nbsp;</div>\n",
##        "title": title
##        }
##        respost=page.insert(blogId='xxxxx',body=payload,isDraft=False).execute() #publishing the new post
##        print("printing the page id:",respost['id'])
    payload={
        "content": "<div dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\">\nEspeciallySports is a sports news portal aims at bringing the latest sports news for the fans that cover MMA, WWE, UFC, Cricket, Football, etc&nbsp;</div>\n",
        "title": "ttile has been changed to uk"
        }
    respost=page.update(blogId='xxxxxx', pageId='xxxxxx', body=payload, publish=True).execute() #updating the existing post/page
    
get_pages_from_jobs()

不在 colab 中工作,他给了我授权链接,它返回令牌以重定向 URL,即本地主机:8090 但 Colap 从未获得令牌密钥......如何手动将密钥从 URL 复制到脚本?有什么帮助吗?有没有办法在 COLAB 中使用博客 API?

标签: pythongoogle-colaboratorygoogle-blogger-api

解决方案


推荐阅读