How to use Blogger API v3 on Google Colab?

25 views Asked by At

I am a blogger and have 120+ posts txt content on my pc. now it is going hard for me to post them on my blog so i search for how to automate it. and find a useful post after many posts

........... https://www.makarablue.com/2023/03/blogger-api-with-python-fully-and.html

I try to use it on Google Colab but what about redirect_uris.

please guide me to make it work.

i try to update the code with help of Colab AI.

!pip install httplib2
!pip install google-api-python-client
!pip install oauth2client

import httplib2
import oauth2client
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run_flow
from googleapiclient import discovery

from google.colab import drive
drive.mount('/content/drive')


# Start the OAuth flow to retrieve credentials
def authorize_credentials():
    CLIENT_SECRET = '/content/drive/MyDrive/Colab Notebooks/BloggerTest/client_secret.json'
    SCOPE = 'https://www.googleapis.com/auth/blogger'
    STORAGE = Storage('/content/drive/MyDrive/Colab Notebooks/BloggerTest/credentials.storage')
    # 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, redirect_uri='http://localhost')
        http = httplib2.Http()
        credentials = run_flow(flow, STORAGE, http=http)
    return credentials

# print(credentials)
def getBloggerService():
    credentials = authorize_credentials()
    http = credentials.authorize(httplib2.Http())
    discoveryUrl = ('https://blogger.googleapis.com/$discovery/rest?version=v3')
    service = discovery.build('blogger', 'v3', http=http, discoveryServiceUrl=discoveryUrl)
    return service

def postToBlogger(payload):
    service = getBloggerService()
    post=service.posts()
    insert=post.insert(blogId='1889850123436343148',body=payload).execute()
    print("Done post!")
    return insert

def buildHtml():
    html = '<h1>Hello world</h1>'
    return html

title = "Testing post 2" 

# print(htmlData)

customMetaData = "This is meta data"
payload={
        "content": buildHtml(),
        "title": title,
        'labels': ['label1','label2'],
        'customMetaData': customMetaData
    }
postToBlogger(payload)

but got this error

usage: colab_kernel_launcher.py [--auth_host_name AUTH_HOST_NAME] [--noauth_local_webserver]
                                [--auth_host_port [AUTH_HOST_PORT ...]]
                                [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
colab_kernel_launcher.py: error: unrecognized arguments: -f /root/.local/share/jupyter/runtime/kernel-9b9e0235-c52b-4335-97a0-ce1b213d2cb8.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2
/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py:3561: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

Please help me out to automate my blog.

0

There are 0 answers