I'm using requests session with oauth2 authentication. Everything works perfectly when I upload small files, but for 4GB file I get token expired error, it looks like the file was uploaded but at the closing session part token was once more validated.
Is there any chance to handle this situation? Upload large file with token refreshed before the session was closed or something?
a sample of the code is below, Thank You very much for any help. Cheers!
import requests
from io import StringIO
from 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)
# Get 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() ? with refreshed token
# session.close() ? with refreshed token
When a token has expired or has been revoked, it can no longer be used to authenticate Git and API requests. It is not possible to restore an expired or revoked token, you or the application will need to create a new token. This article explains the possible reasons your GitHub token might be revoked or expire.
If you experience an error message that states "Token Expired", this is letting you know the system has timed out and will need to be refreshed.
Use the Authorization Code Flow to get both a refresh token and access token. If your application is authorized for programmatic refresh tokens, the following fields are returned when you exchange the authorization code for an access token: refresh_token — Your refresh token for the application.
If you want to have valid access tokens for more time you can also request for refresh tokens and use them to generate new access tokens whenever the old one expires. Generally access tokens are valid for 1 hour, you can maintain a timer and generate a new access token every time your timer reaches 60 minutes. That way you can have a valid access token for longer sessions.
You have to use grant_type=refresh_token
https://www.rfc-editor.org/rfc/rfc6749#section-6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With