Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Ebay - 'error': 'invalid_grant', 'error_description': 'the provided authorization refresh token is invalid or was issued to another client'

simong1993

Gold Coder
Staff Team
Guardian
Up until last month, my code was working perfectly. I was able to get a refresh token from eBay using AuthToken but for some reason, it just stopped. I have tried getting a new AuthToken with the same results and i have got the token manually from eBay and it works. I am at a loss as to what has happened and was wondering if someone could have a look at my code for me and see if I am doing something wrong.


Python:
import datetime
import base64
import requests
import mysql.connector as mysql

class Access_Token:

    def __init__(self, User, Database,UserDetails):

        #print("Get/Check Token",User)
        self.Token = self.CheckUser(User,Database,UserDetails)

    def CheckUser(self, User,Database,UserDetails):
        AuthToken = ""
        CurrentTime = datetime.datetime.now()
        ExpireTime = CurrentTime + datetime.timedelta(minutes=4)  # Current time pluss 5 mins for expiry

        if User['token'] == None:  # If no Token then make one
            print("Make a new token as we have none for", Database['Database'], "-",Database['User'])
            AuthToken = self.GetOAuth(User,UserDetails)
            UserID = User['id']
            db_connection = mysql.connect(host=Database['Host'], database=Database['Database'], user=Database['User'], password=Database['Password'])  # Users
            cursor = db_connection.cursor()
            sql = "UPDATE Users SET AuthToken = %s, AuthTokenCreated = %s WHERE id=%s"
            val = (AuthToken, ExpireTime, UserID)
            cursor.execute(sql, val)
            db_connection.commit()
            cursor.close()
            return AuthToken

        elif User['AuthTokenCreated'] < str(CurrentTime):  # if token expires get a new one
            print("Token Expired for", Database['Database'], "-",Database['User'])
            AuthToken = self.GetOAuth(User,UserDetails)
            UserID = User['id']
            db_connection = mysql.connect(host=Database['Host'], database=Database['Database'], user=Database['User'], password=Database['Password'])  # Users
            cursor = db_connection.cursor()
            sql = "UPDATE Users SET AuthToken = %s, AuthTokenCreated = %s WHERE id=%s"
            val = (AuthToken, ExpireTime, UserID)
            cursor.execute(sql, val)
            db_connection.commit()
            cursor.close()
            return AuthToken

        else:
            print("Token Good for", Database['Database'], "-",Database['User'])
            AuthToken = User['AuthToken']
            return AuthToken

    def GetOAuth(self, User,UserDetails):
        encodedData = base64.b64encode(bytes(f"{UserDetails['client_id']}:{UserDetails['client_secret']}", "ISO-8859-1")).decode("ascii")
        authorization_header_string = f"Authorization: Basic {encodedData}"

        headers = {"Authorization": "Basic " + encodedData, "Content-Type": "application/x-www-form-urlencoded"}

        data = {
            'grant_type': 'refresh_token',
            'refresh_token': User['token'],
            'redirect_uri': User['redirect_uri'],
        }

        response = requests.post('https://api.ebay.com/identity/v1/oauth2/token', headers=headers, data=data)
        data = response.json()
        print("UserDetails", UserDetails)
        print("===================")
        print("User", User)
        print("===================")
        print("Headers", headers)
        print("===================")
        print("Response", response)
        print("===================")
        print("data", data)
        print("===================")
        print("Getting New Token",response,data['access_token'])
        print("===================")
        print(data)
        return data['access_token']
 
Up until last month, my code was working perfectly. I was able to get a refresh token from eBay using AuthToken but for some reason, it just stopped. I have tried getting a new AuthToken with the same results and i have got the token manually from eBay and it works. I am at a loss as to what has happened and was wondering if someone could have a look at my code for me and see if I am doing something wrong.


Python:
import datetime
import base64
import requests
import mysql.connector as mysql

class Access_Token:

    def __init__(self, User, Database,UserDetails):

        #print("Get/Check Token",User)
        self.Token = self.CheckUser(User,Database,UserDetails)

    def CheckUser(self, User,Database,UserDetails):
        AuthToken = ""
        CurrentTime = datetime.datetime.now()
        ExpireTime = CurrentTime + datetime.timedelta(minutes=4)  # Current time pluss 5 mins for expiry

        if User['token'] == None:  # If no Token then make one
            print("Make a new token as we have none for", Database['Database'], "-",Database['User'])
            AuthToken = self.GetOAuth(User,UserDetails)
            UserID = User['id']
            db_connection = mysql.connect(host=Database['Host'], database=Database['Database'], user=Database['User'], password=Database['Password'])  # Users
            cursor = db_connection.cursor()
            sql = "UPDATE Users SET AuthToken = %s, AuthTokenCreated = %s WHERE id=%s"
            val = (AuthToken, ExpireTime, UserID)
            cursor.execute(sql, val)
            db_connection.commit()
            cursor.close()
            return AuthToken

        elif User['AuthTokenCreated'] < str(CurrentTime):  # if token expires get a new one
            print("Token Expired for", Database['Database'], "-",Database['User'])
            AuthToken = self.GetOAuth(User,UserDetails)
            UserID = User['id']
            db_connection = mysql.connect(host=Database['Host'], database=Database['Database'], user=Database['User'], password=Database['Password'])  # Users
            cursor = db_connection.cursor()
            sql = "UPDATE Users SET AuthToken = %s, AuthTokenCreated = %s WHERE id=%s"
            val = (AuthToken, ExpireTime, UserID)
            cursor.execute(sql, val)
            db_connection.commit()
            cursor.close()
            return AuthToken

        else:
            print("Token Good for", Database['Database'], "-",Database['User'])
            AuthToken = User['AuthToken']
            return AuthToken

    def GetOAuth(self, User,UserDetails):
        encodedData = base64.b64encode(bytes(f"{UserDetails['client_id']}:{UserDetails['client_secret']}", "ISO-8859-1")).decode("ascii")
        authorization_header_string = f"Authorization: Basic {encodedData}"

        headers = {"Authorization": "Basic " + encodedData, "Content-Type": "application/x-www-form-urlencoded"}

        data = {
            'grant_type': 'refresh_token',
            'refresh_token': User['token'],
            'redirect_uri': User['redirect_uri'],
        }

        response = requests.post('https://api.ebay.com/identity/v1/oauth2/token', headers=headers, data=data)
        data = response.json()
        print("UserDetails", UserDetails)
        print("===================")
        print("User", User)
        print("===================")
        print("Headers", headers)
        print("===================")
        print("Response", response)
        print("===================")
        print("data", data)
        print("===================")
        print("Getting New Token",response,data['access_token'])
        print("===================")
        print(data)
        return data['access_token']
have you checked the docs for any updates?
 
Surely if your code has always worked fine, and you didn't change it, it cannot suddenly be wrong ? Seems more likely they been changing stuff at Ebay. Have you tried the Ebay Community ?
 
Surely if your code has always worked fine, and you didn't change it, it cannot suddenly be wrong ? Seems more likely they been changing stuff at Ebay. Have you tried the Ebay Community ?
Ebay dev community is pants and Ebay is just as bad, i spoke to them and they only change they have made is they now use Oauth as well as Auth where i was Auth but they have assured me that will no cause me any issues :S
 
Well... you had to check, right ?
No clue what's up here. But lacking better ideas, I'd use Process Monitor (or strace if you are on Linux) to find out if it's opening any temporary (cookie-like ?) files, and try removing these. Worth a try I guess.
 

New Threads

Buy us a coffee!

Back
Top Bottom