Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
CSC190 Group 6
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Imam M. Jah
CSC190 Group 6
Commits
ce9d5676
Commit
ce9d5676
authored
Oct 17, 2024
by
Imam M. Jah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
app1
parent
5a50001d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
0 deletions
+70
-0
app1.py
src/app1.py
+0
-0
kroger.py
src/kroger.py
+70
-0
No files found.
src/app.py
→
src/app
1
.py
View file @
ce9d5676
File moved
src/kroger.py
0 → 100644
View file @
ce9d5676
import
requests
from
requests.auth
import
HTTPBasicAuth
# Replace these with your actual Kroger API credentials
CLIENT_ID
=
'your_client_id'
CLIENT_SECRET
=
'your_client_secret'
# OAuth 2.0 token URL and Kroger API product search URL
TOKEN_URL
=
'https://api.kroger.com/v1/connect/oauth2/token'
PRODUCT_API_URL
=
'https://api.kroger.com/v1/products'
# Step 1: Get an OAuth 2.0 Access Token
def
get_access_token
():
# Request headers and data for token generation
headers
=
{
'Content-Type'
:
'application/x-www-form-urlencoded'
,
}
data
=
{
'grant_type'
:
'client_credentials'
,
'scope'
:
'product.compact'
,
# Scope for product data
}
# Send the POST request to obtain an access token
response
=
requests
.
post
(
TOKEN_URL
,
headers
=
headers
,
data
=
data
,
auth
=
HTTPBasicAuth
(
CLIENT_ID
,
CLIENT_SECRET
))
if
response
.
status_code
==
200
:
token_data
=
response
.
json
()
return
token_data
[
'access_token'
]
else
:
print
(
f
"Failed to get access token: {response.status_code}, {response.text}"
)
return
None
# Step 2: Search for Products Using Kroger API
def
search_products
(
token
,
query
):
headers
=
{
'Authorization'
:
f
'Bearer {token}'
,
'Content-Type'
:
'application/json'
,
}
params
=
{
'filter.term'
:
query
,
# Search term, e.g., "milk"
'filter.locationId'
:
'01234567'
# Replace with a valid location ID
}
# Send the GET request to search for products
response
=
requests
.
get
(
PRODUCT_API_URL
,
headers
=
headers
,
params
=
params
)
if
response
.
status_code
==
200
:
return
response
.
json
()
else
:
print
(
f
"Failed to fetch products: {response.status_code}, {response.text}"
)
return
None
# Main code to test the API
if
__name__
==
"__main__"
:
# Step 1: Get Access Token
token
=
get_access_token
()
if
token
:
print
(
f
"Access token: {token}"
)
# Step 2: Search for products (e.g., search for "milk")
products
=
search_products
(
token
,
'milk'
)
if
products
:
print
(
"Products found:"
)
for
product
in
products
[
'data'
]:
print
(
f
"Product ID: {product['productId']}, Description: {product['description']}"
)
else
:
print
(
"No products found or an error occurred."
)
else
:
print
(
"Failed to get access token."
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment