Installation Blogger APIs Client Library for Python CMD> pip install --upgrade google-api-python-client Ref: developers.google.com (base) C:\Users\ashish\Desktop>pip install oauth2client (base) C:\Users\ashish\Desktop\blogger>pip show google-api-python-client Name: google-api-python-client Version: 1.12.3 Summary: Google API Client Library for Python Home-page: https://github.com/googleapis/google-api-python-client/ Author: Google LLC Author-email: googleapis-packages@google.com License: Apache 2.0 Location: d:\programfiles\anaconda3\lib\site-packages Requires: six, google-auth-httplib2, google-auth, google-api-core, uritemplate, httplib2 Required-by: (base) C:\Users\ashish\Desktop\blogger>pip show oauth2client Name: oauth2client Version: 4.1.3 Summary: OAuth 2.0 client library Home-page: http://github.com/google/oauth2client/ Author: Google Inc. Author-email: jonwayne+oauth2client@google.com License: Apache 2.0 Location: d:\programfiles\anaconda3\lib\site-packages Requires: pyasn1, six, rsa, pyasn1-modules, httplib2 Required-by: == == == == == Basic structure of the "client_secrets.json" is as written below: Ref: GitHub { "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" } } Our "client_secrets.json" file looks like this: { "installed": { "client_id": "0...9-a...z.apps.googleusercontent.com", "project_id": "banded...", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_secret": "2.....v", "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "http://localhost"] } } == == == == == About authorization protocols Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported. If your application uses Google Sign-In, some aspects of authorization are handled for you. You can create "OAuth Client ID" from here: developers.google.com You can also create a new "Project". We will select already created "My Project". We will select the "OAuth Client" as "Desktop App": == == == == == Error if the 'client_secrets.json' file is not present alongside "blogger.py": (base) C:\Users\ashish\Desktop>python blogger.py The client secrets were invalid: ('Error opening file', 'client_secrets.json', 'No such file or directory', 2) WARNING: Please configure OAuth 2.0 To make this sample run you will need to populate the client_secrets.json file found at: client_secrets.json with information from the APIs Console [ https://code.google.com/apis/console ]. == == == == == Error on placing "empty (0KB)" file named "client_secrets.json": (base) C:\Users\ashish\Desktop\blogger>python blogger_v2.py Traceback (most recent call last): File "blogger_v2.py", line 43, in [module] main(sys.argv) File "blogger_v2.py", line 12, in main scope='https://www.googleapis.com/auth/blogger') File "D:\programfiles\Anaconda3\lib\site-packages\googleapiclient\sample_tools.py", line 88, in init client_secrets, scope=scope, message=tools.message_if_missing(client_secrets) File "D:\programfiles\Anaconda3\lib\site-packages\oauth2client\_helpers.py", line 133, in positional_wrapper return wrapped(*args, **kwargs) File "D:\programfiles\Anaconda3\lib\site-packages\oauth2client\client.py", line 2135, in flow_from_clientsecrets cache=cache) File "D:\programfiles\Anaconda3\lib\site-packages\oauth2client\clientsecrets.py", line 165, in loadfile return _loadfile(filename) File "D:\programfiles\Anaconda3\lib\site-packages\oauth2client\clientsecrets.py", line 122, in _loadfile obj = json.load(fp) File "D:\programfiles\Anaconda3\lib\json\__init__.py", line 296, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) File "D:\programfiles\Anaconda3\lib\json\__init__.py", line 348, in loads return _default_decoder.decode(s) File "D:\programfiles\Anaconda3\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "D:\programfiles\Anaconda3\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) == == == == == Before authentication: C:\Users\ashish\Desktop\blogger>tree /f C:. blogger_v1.py client_secrets.json No subfolders exist After authentication (the authentication steps are shown below), a 'blogger.dat' file is created: C:\Users\ashish\Desktop\blogger>tree /f C:. blogger.dat blogger_v1.py client_secrets.json No subfolders exist == == == == == When we run the application the application the first time, it opens the "Google's User Authentication" window to give access to the application to the Blogger for a Google account by logging into the respective Google Account. Last prompt will ask you to confirm your choice: Once the sign-in is complete it opens a Broswer Window (our browser is Chrome) as shown below: Message: Application flow has been completed == == == == == CODE V1 #!/usr/bin/env python # -*- coding: utf-8 -*- """Simple command-line sample for Blogger. Command-line application that retrieves the users blogs and posts. Usage: $ python blogger.py You can also get help on all the command-line flags the program understands by running: $ python blogger.py --help To get detailed log output run: $ python blogger.py --logging_level=DEBUG """ from __future__ import print_function import sys from oauth2client import client from googleapiclient import sample_tools def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'blogger', 'v3', __doc__, __file__, scope='https://www.googleapis.com/auth/blogger') try: 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']) blogs = service.blogs() # Retrieve the list of Blogs this user has write privileges on thisusersblogs = blogs.listByUser(userId='self').execute() for blog in thisusersblogs['items']: print('The blog named \'%s\' is at: %s' % (blog['name'], blog['url'])) posts = service.posts() # List the posts for each blog this user has for blog in thisusersblogs['items']: print('The posts for %s:' % blog['name']) request = posts.list(blogId=blog['id']) while request != None: posts_doc = request.execute() if 'items' in posts_doc and not (posts_doc['items'] is None): for post in posts_doc['items']: print(' %s (%s)' % (post['title'], post['url'])) request = posts.list_next(request, posts_doc) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run' 'the application to re-authorize') if __name__ == '__main__': main(sys.argv) == == == == == (base) CMD>python blogger_v1.py D:\programfiles\Anaconda3\lib\site-packages\oauth2client\_helpers.py:255: UserWarning: Cannot access blogger.dat: No such file or directory warnings.warn(_MISSING_FILE_MESSAGE.format(filename)) Your browser has been opened to visit: https://accounts.google.com/o/oauth2/auth?client_id=1... If your browser is on a different machine then exit and re-run this application with the command-line parameter --noauth_local_webserver Authentication successful. This user's display name is: Ashish Jain The blog named 'survival8' is at: http://survival8.blogspot.com/ The blog named 'ashish' is at: http://ashish.blogspot.com/ The posts for survival8: Difficult Conversations. How to Discuss What Matters Most (D. Stone, B. Patton, S. Heen, 2010) (http://survival8.blogspot.com/2020/09/douglas-stone-b-patton-s-heen-difficult.html) ... Is having a water fountain a sensible thing to do? (http://survival8.blogspot.com/2016/11/is-having-water-fountain-sensible-thing_21.html) The posts for ashish: ... == == == == == 'Cloud Resource Manager' Screenshot Showing Our Usage == == == == == References GitHub % Blogger: google-api-python-client % blogger.py Google API Console % Google API Console % Dashboard % Cloud Resource Manager Documentation % Blogger APIs Client Library for Python % Blogger API: Using the API % Blogger API v3 (Instance Methods) % Blogger API v3 . posts (Instance Methods)
Sunday, October 4, 2020
Set up Google OAuth 2.0 Authentication For Blogger and Retrieve Blog Posts
Labels:
Cloud,
Python,
Technology,
Web Development
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment