DEV Community

Cover image for Using Python to Access Google Bard: A Simple Guide on Session Cookies
Vengat
Vengat

Posted on

1

Using Python to Access Google Bard: A Simple Guide on Session Cookies

Google Bard is a fantastic tool many have been waiting to explore programmatically. While direct API access might not be available to everyone, thanks to developers like Daniel Park, we have workarounds. He introduced the bardapi Python package, which leverages session cookies to interact with Google Bard. In this guide, I'll walk you through how to use this package.

Getting Started:
1. Prerequisites:

  • Python 3.x
  • Google Bard account
  • Basic knowledge of web developer tools in browsers

2. Installation:
If you haven’t already, install the bardapi package:

pip install bardapi
Enter fullscreen mode Exit fullscreen mode

Retrieving Your Session Cookies:

Session cookies are essential for maintaining a user session, which the bardapi package uses to authenticate against Google Bard.

Steps to retrieve session cookies:

  1. Navigate to the Google Bard website on a browser and log in.
  2. Press F12 or right-click on the webpage and select 'Inspect' to open developer tools.
  3. Navigate to the Application tab.
  4. In the left sidebar, expand the Cookies section under Storage.
  5. Select accounts.google.com or a similar domain. Here you'll find a list of cookies.
  6. Look for the __Secure-1PSID cookie and note its value.
from bardapi import Bard
import os

#Replace BBBBBBB with the values you get from __Secure-1PSID 
os.environ['_BARD_API_KEY']="BBBBBBB"

# set your input text
input_text = "what is google bard?"

print(Bard().get_answer(input_text)['content'])
Enter fullscreen mode Exit fullscreen mode

Above code may not work if you have multi session cookies, if that case replace above code with below,

You need to set multi cookie value. Using Bard Cookies Object.

from bardapi import BardCookies

cookie_dict = {
    "__Secure-1PSID": "Your ID",
    "__Secure-1PSIDTS": "Your Id",
    # Any cookie values you want to pass session object.
}

bard = BardCookies(cookie_dict=cookie_dict)
print(bard.get_answer('Tell me more about Bard"))
Enter fullscreen mode Exit fullscreen mode

Image description

I hope you found this guide on accessing Google Bard using Python and session cookies insightful. Your feedback fuels my passion for sharing knowledge, so if you appreciated this article, please give it a thumbs up and share your thoughts in the comments below.

For more insights and to stay connected, you can find me on:

LinkedIn
Teckiy
Twitter
Thank you for reading, and I look forward to engaging with you on these platforms!

Billboard image

Imagine monitoring that's actually built for developers

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay