This page describes how to add search functionality to a website using the search widget.
Before you begin
Before you add a search widget to your web page, be sure to configure your search widget, starting with Configure results for the search widget. Note that it's not possible to adjust the styling of the search widget in your web page.
Procedures
The procedure for adding the search widget to your web page differs depending on the authorization type you choose:
Authorization token: The widget uses a JWT or OAuth token provided by your codebase. You must have your own server that you can use to generate a token. The auth token is used to make an API call on behalf of the user or service account. When generating the auth token, use the OAuth scope
https://www.googleapis.com/auth/cloud-platform
.For server-to-server interactions with service accounts: See Using OAuth 2.0 for Server to Server Applications in the authorization documentation.
For JavaScript web applications: See OAuth 2.0 for Client-side Web Applications in the authorization documentation.
Public access: The widget won't restrict who can get results from a search query.
Add a widget that uses an authorization token
In the Google Cloud console, go to the Agent Builder page.
Click the name of the app that you want to edit.
Click Integration.
Click the Widget tab.
Select JWT or OAuth based as the widget authorization type.
Specify the domain name for the widget page:
Enter the domain name for the page where the widget will appear.
For example, if you are going to copy the widget to the pages
example.com/ai.html
andcymbal.example.com/search.html
, enterexample.com
as the domain. If you only want the widget to work on a page in the subdomain, entercymbal.example.com
.If you want to test the widget on your localhost, enter
localhost
.Click Add.
Repeat step 6 for each domain where the widget will appear.
Click Save.
Copy the code snippet provided in the Copy the following code to your web application section.
In your codebase, generate an authorization token.
To pass the authorization token to your widget, use the second code snippet, "
// Set authorization token
", provided in the Copy the following code to your web application section and replace the text<JWT or OAuth token provided by you backend>
with your authorization token.Periodically set a fresh token by repeating the previous step before the current token expires.
Add a widget that has public access
In the Google Cloud console, go to the Agent Builder page.
Click the name of the app that you want to edit.
For public access, make sure that the search app isn't associated with data store under access control.
Click Integration.
Click the Widget tab.
Select Public Access as the widget authorization type.
Specify the domain name for the widget page:
Enter the domain name for the page where the widget will appear.
For example, if you are going to copy the widget to the pages
example.com/ai.html
andcymbal.example.com/search.html
, enterexample.com
as the domain. If you only want the widget to work on pages in the subdomain, entercymbal.example.com
.If you want to test the widget on your localhost, enter
localhost
.Click Add.
Repeat step 6 for each domain where the widget will appear.
Click Save.
Copy the code snippet provided in the Copy the following code to your web application section and paste the code snippet into your web page.