Last active
July 30, 2023 03:46
-
-
Save vpatel9202/3963da81283488c76962167585835740 to your computer and use it in GitHub Desktop.
Obtain access token and refresh token for Box API OAuth 2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See https://forum.rclone.org/t/box-getting-403-errors-when-using-chunker-working-fine-without-it/40388/22?u=vashp2029 | |
import requests | |
import urllib.parse | |
import json | |
# Define your Box app's client ID, client secret, and redirect URI | |
CLIENT_ID = 'YOUR_CLIENT_ID' | |
CLIENT_SECRET = 'YOUR_CLIENT_SECRET' | |
REDIRECT_URI = 'http://localhost:8080' | |
AUTH_URL = 'https://account.box.com/api/oauth2/authorize' | |
TOKEN_URL = 'https://api.box.com/oauth2/token' | |
params = { | |
'response_type': 'code', | |
'client_id': CLIENT_ID, | |
'state': 'random_string', # Replace this with a real random string | |
'redirect_uri': REDIRECT_URI, | |
} | |
print(f'Go to the following URL: {AUTH_URL}?{urllib.parse.urlencode(params)}') | |
# Step 2: User authorizes your app... | |
# Step 3: Handle the redirect | |
code = input('Enter the code parameter from the redirect URL: ') | |
# Step 4: Exchange the code for tokens | |
data = { | |
'grant_type': 'authorization_code', | |
'code': code, | |
'client_id': CLIENT_ID, | |
'client_secret': CLIENT_SECRET, | |
} | |
response = requests.post(TOKEN_URL, data=data) | |
tokens = response.json() | |
# Create a new dictionary with only the access_token and refresh_token | |
tokens_filtered = { | |
'access_token': tokens['access_token'], | |
'refresh_token': tokens['refresh_token'], | |
} | |
# Print the filtered tokens in JSON format | |
print(json.dumps(tokens_filtered)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment