Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Token expire before file was uploaded

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
like image 826
k.rozycki Avatar asked Jul 28 '17 10:07

k.rozycki


People also ask

What happens when token expires?

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.

Why does it say token has expired?

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.

How do you refresh an expired token?

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.


Video Answer


1 Answers

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

like image 175
Venkatesh Marepalli Avatar answered Oct 16 '22 14:10

Venkatesh Marepalli