2016-06-16 15 views
2

Google API gdataのブログ機能を使用しようとしています。私はドキュメンテーションの後にベストを尽くしたが、とてもうまくいかなかった。誰も私にどのように私はGoogleのブロガーAPIを使用することができます教えていただけますか?私のコードはかなり混乱していて、今私は手がかりから外れています。PythonでGoogle Blogger APIを使用するには?

EDIT FULL WORKING CODE :):

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']) 
+0

(http://stackoverflow.com/help/how-to-ask)[HOW-TO-お願い】読み、その後、追加してください。あなたの質問に関連する情報"私は非常にひどく失敗しました"とは、特定のプログラミング上の問題ではありません。 – Mailerdaimon

+0

私はちょうどこのAPIを使用する手がかりがありません: 'https:// developers.google.com/blogger/docs/3.0/api-lib/python'これらの関数を使いたい:' https:// developers.google.com/apis-explorer /#p/blogger/v3/'しかし、私は' aouth 2.0'で動作させる方法を知らない。 – ryspbsk

+0

OAuthガイドを読んだことはありますか? https://developers.google.com/api-client-library/python/guide/aaa_oauth – Mailerdaimon

答えて

2

私自身は解決策を見つけることを試みたが、私は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/'} 
関連する問題