您可以尝试以下解决方案。
请确保所有以下导入都在那里。
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run
像这样设置你的 client_secrets.JSON
像这样设置 client_secrets.json
{
"web": {
"client_id": "[[INSERT CLIENT ID HERE]]",
"client_secret": "[[INSERT CLIENT SECRET HERE]]",
"redirect_uris": [],
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token"
}
}
为了将来参考,您可以将凭据存储在文件blogger.dat
中以便更快地处理
FLOW = flow_from_clientsecrets(Path_to_client_secrets.json,scope='https://www.googleapis.com/auth/blogger',message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage('blogger.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run(FLOW, storage)
一旦凭据都设置好了。是时候发帖了!因此我们创建了一个 httplib2.Http 对象来处理我们的 HTTP 请求并使用我们良好的凭据对其进行授权。
http = httplib2.Http()
http = credentials.authorize(http)
service = build("blogger", "v2", http=http)
完成后,我们构建博客正文并发布
try:
body = {
"kind": "blogger#post",
"id": "6814573853229626501",
"title": "posted via python",
"content":"<div>hello world test</div>"
}
request = service.posts().insert(your_blogId_ID,body)
response = request.execute()
print response
except AccessTokenRefreshError:
print ("The credentials have been revoked or expired, please re-run the application to re-authorize")
希望这可以帮助。