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
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:
- Navigate to the Google Bard website on a browser and log in.
- Press F12 or right-click on the webpage and select 'Inspect' to open developer tools.
- Navigate to the Application tab.
- In the left sidebar, expand the Cookies section under Storage.
- Select accounts.google.com or a similar domain. Here you'll find a list of cookies.
- 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'])
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"))
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!
Top comments (0)