JacobElliott
JacobElliott

Reputation: 127

How can I use Python Keyring in conjunction with Box API OAuth2?

I'm pretty new to Python and programming and I am trying to figure out how to automate the box.com authentication process and it's kicking my butt. Any help would be appreciated!

I have this code below, which obviously wasn't mine but came from a tutorial. I am trying to figure out the

keyring.get_password('Box_Auth', '[email protected]')

I'm thinking the [email protected] is my redirect URI? But I'm not sure what it is looking for when it asks for the Box_Auth.

Here is the full code

"""An example of Box authentication with external store"""

import keyring
from boxsdk import OAuth2
from boxsdk import Client

CLIENT_ID = ''
CLIENT_SECRET = ''


def read_tokens():
"""Reads authorisation tokens from keyring"""
# Use keyring to read the tokens
auth_token = keyring.get_password('Box_Auth', '[email protected]')
refresh_token = keyring.get_password('Box_Refresh', '[email protected]')
return auth_token, refresh_token


def store_tokens(access_token, refresh_token):
"""Callback function when Box SDK refreshes tokens"""
# Use keyring to store the tokens
keyring.set_password('Box_Auth', '[email protected]', access_token)
keyring.set_password('Box_Refresh', '[email protected]', refresh_token)


def main():
"""Authentication against Box Example"""

# Retrieve tokens from secure store
access_token, refresh_token = read_tokens()

# Set up authorisation using the tokens we've retrieved
oauth = OAuth2(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
access_token=access_token,
refresh_token=refresh_token,
store_tokens=store_tokens,
)

# Create the SDK client
client = Client(oauth)
# Get current user details and display
current_user = client.user(user_id='me').get()
print('Box User:', current_user.name)

Again, I would really appreciate any help!

Upvotes: 2

Views: 646

Answers (1)

Ole EH Dufour
Ole EH Dufour

Reputation: 3240

I was having the exact same issue.

You will need an access token and a refresh token. Read here how to generate those.

Upvotes: 1

Related Questions