box / box-python-sdk

Box SDK for Python
http://opensource.box.com/box-python-sdk/
Apache License 2.0
418 stars 215 forks source link

Refresh token issue on OAuth2 authentication #730

Closed RevathiB12 closed 1 year ago

RevathiB12 commented 2 years ago

The requirement is that we need to acces a box folder of very large data (1 million records approx). I am facing an issue when I try to refresh token using boxsdk.

1.Issue noticed : At the expiry of the tokens (60-80 minutes) Box API fails with the below error message and the script terminates abruptly .

Client id — ihx0fcsdzdxuw42gahauskyhd2v57sed API key — OAuth2.0 SDK version —BoxSDK (3.3.0)

  1. Below are the logs . Authentication Issue happening randomly even though the Refersh and Access Tokens are valid, Below is the error log

"POST https://api.box.com/oauth2/token" 400 69 {'Date': 'Wed, 18 May 2022 17:48:22 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Strict-Transport-Security': 'max-age=31536000', 'Set-Cookie': 'box_visitor_id=62853165d51f65.14434401; expires=Thu, 18-May-2023 17:48:21 GMT; Max-Age=31536000; path=/; domain=.box.com; secure, bv=OPS-45228; expires=Wed, 25-May-2022 17:48:21 GMT; Max-Age=604800; path=/; domain=.app.box.com; secure, cn=92; expires=Thu, 18-May-2023 17:48:21 GMT; Max-Age=31536000; path=/; domain=.app.box.com; secure, site_preference=desktop; path=/; domain=.box.com; secure', 'Cache-Control': 'no-store'} {'error': 'invalid_grant', 'error_description': 'Invalid refresh token'}

Message: Invalid refresh token Status: 400 URL: https://api.box.com/oauth2/token Method: POST Headers: {'Date': 'Wed, 18 May 2022 17:48:22 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Strict-Transport-Security': 'max-age=31536000', 'Set-Cookie': 'box_visitor_id=62853165d51f65.14434401; expires=Thu, 18-May-2023 17:48:21 GMT; Max-Age=31536000; path=/; domain=.box.com; secure, bv=OPS-45228; expires=Wed, 25-May-2022 17:48:21 GMT; Max-Age=604800; path=/; domain=.app.box.com; secure, cn=92; expires=Thu, 18-May-2023 17:48:21 GMT; Max-Age=31536000; path=/; domain=.app.box.com; secure, site_preference=desktop; path=/; domain=.box.com; secure', 'Cache-Control': 'no-store'}

Note: I am using environment python3. Can you pls help me on this?

RevathiB12 commented 2 years ago

Please find the code snippet here:

import os from threading import Thread, Event import webbrowser from wsgiref.simple_server import WSGIServer, WSGIRequestHandler, make_server from boxsdk import OAuth2 import configparser from configparser import ConfigParser import sched, time import schedule

def authenticate(CLIENT_ID,CLIENT_SECRET,REFRESH_TOKEN,oauth_class=OAuth2):

Needed to get new refresh token

oauth = oauth_class( client_id=CLIENT_ID, client_secret=CLIENT_SECRET, refresh_token=REFRESH_TOKEN, store_tokens= Store_New_Logs, )

try: access_token, refresh_token = oauth.refresh(None)

print(refresh_token)

#print (access_token)
return access_token, refresh_token

except Exception as e: print(e) print ('Refresh token expired')

Needed to get new refresh token

def Creds_Fetcher(): try: global creds_path

creds_path = '/Users/vishal.tp/Desktop/final/flask/CBot_Box_Creds.cfg'

cwd = os.getcwd() creds_path = os.path.join(cwd,'Search-Test_Creds.cfg') config = configparser.ConfigParser() config.read(creds_path) ACCESS_TOKEN_Old = config.get('Search','ACCESS_TOKEN') REFRESH_TOKEN_Old = config.get('Search','REFRESH_TOKEN') print ('ACCESS_TOKEN_Old',ACCESS_TOKEN_Old) print ('REFRESH_TOKEN_Old',REFRESH_TOKEN_Old) return ACCESS_TOKEN_Old,REFRESH_TOKEN_Old except Exception as e: print ('Nope',e)

def Store_New_Logs(REFRESH_TOKEN_NEW,ACCESS_TOKEN_NEW): try: global creds_path config = configparser.ConfigParser() config.read(creds_path) config.set('Search','ACCESS_TOKEN',ACCESS_TOKEN_NEW) config.set('Search','REFRESH_TOKEN',REFRESH_TOKEN_NEW) with open(creds_path, 'w+') as configfile: config.write(configfile) except Exception as e: print (e)

def Token_Refresher(): ACCESS_TOKEN_Old,REFRESH_TOKEN_Old = Creds_Fetcher() CLIENT_ID = 'xxxx' # Insert Box client ID here CLIENT_SECRET = 'xxx' # Insert Box client secret here REFRESH_TOKEN = REFRESH_TOKEN_Old ACCESS_TOKEN_NEW,REFRESH_TOKEN_NEW = authenticate(CLIENT_ID,CLIENT_SECRET,REFRESH_TOKEN) print ('ACCESS_TOKEN_NEW',ACCESS_TOKEN_NEW) print ('REFRESH_TOKEN_NEW',REFRESH_TOKEN_NEW) Store_New_Logs(REFRESH_TOKEN_NEW,ACCESS_TOKEN_NEW) return ACCESS_TOKEN_NEW

Token_Refresher()

RevathiB12 commented 2 years ago

Are there any updates /solutions for this issue?

mhagmajer commented 2 years ago

Thanks for posting! We're looking into this issue

RevathiB12 commented 2 years ago

Sure Marcin!

arjankowski commented 2 years ago

hello @RevathiB12 ,

I think the problem is with the method def Store_New_Logs (REFRESH_TOKEN_NEW, ACCESS_TOKEN_NEW) , which was passed to the OAuth constructor as the store_tokens callback parameter. This ensures that this method will be called every time the token is retrieved or refreshed. Which is okay.

But in the def Store_New_Logs (REFRESH_TOKEN_NEW, ACCESS_TOKEN_NEW) method, you named the first parameter REFRESH_TOKEN_NEW and the second parameter ACCESS_TOKEN_NEW. When in fact the callback is invoked with the opposite parameters order, access_token as the first argument andrefresh_token as the second:

self._store_tokens_callback (access_token, refresh_token)

To fix that you should rename the parameters so that they look like this:

Store_New_Logs (ACCESS_TOKEN_NEW, REFRESH_TOKEN_NEW),

Then your code should work fine.

Please let me know if it helped

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not been updated in the last 30 days. It will be closed if no further activity occurs within the next 7 days. Feel free to reach out or mention Box SDK team member for further help and resources if they are needed.

stale[bot] commented 1 year ago

This issue has been automatically closed due to maximum period of being stale. Thank you for your contribution to Box Python SDK and feel free to open another PR/issue at any time.