python 2.7 - Token expire before file was uploaded -
i'm using requests session oauth2 authentication. works when upload small files, 4gb file token expired error, looks file uploaded @ closing session part token once more validated.
is there chance handle situation? upload large file token refreshed before session closed or something?
a sample of code below, thank help. cheers!
import requests io import stringio requests_toolbelt.multipart.encoder import multipartencoder token_payload = { 'grant_type': 'password', 'client_id': '###', 'client_secret': '###', 'username': '###', 'password': '####' } def get_token(): response = requests.post( 'https://oauth/token', params=token_payload) response_data = response.json() token = response_data.get('access_token') return token # create test file mb = 1024 ** 2 gb = mb * 1024 encoded_string = 'x' * 4 * gb file_test = stringio() file_test.write(encoded_string) # token token = get_token() # create form multipart_data = multipartencoder( fields={ '--': ('4gb_test.txt', file_test, 'text/plain'), 'id': '2217', 'filefielddefid': '4258', } ) # create headers headers = { "authorization": "bearer {}".format(token), 'content-type': multipart_data.content_type } session = requests.session() response = session.post( 'https://oauth2/rest/external/item/multipartupdate/byid', headers=headers, data=multipart_data, ) print(response) # <response [401]> print(response.content) # b'{"error":"invalid_token","error_description":"access token expired: 0f7f6bd9-4e21-407f-4a78347711a9"}' # response.close() ? refreshed token # session.close() ? refreshed token
if want have valid access tokens more time can request refresh tokens , use them generate new access tokens whenever old 1 expires. access tokens valid 1 hour, can maintain timer , generate new access token every time timer reaches 60 minutes. way can have valid access token longer sessions.
you have use grant_type=refresh_token
https://tools.ietf.org/html/rfc6749#section-6
Comments
Post a Comment