简体   繁体   中英

Django Instagram authentication doesn't work as expected

I have pushed my django project onto pythonanywhere , a free hosting website. The code works fine in my local and onto the web as well, but somehow have an issue with instagram authentication.

Instagram uses an oauth2 authentication system, in which when the user allows your application, it redirects the user to a specified URI and appends the code inside the URL which further can be used to authenticate the user.

Instagram doesn't allow localhosts as valid redirect URL, therefore for the testing I have used something like this

InstaAuth.html

<script>
        history.pushState(null, null, '?code=AQD_4...q6#_');
        function reloadThePage() {
            window.location.reload();
        }
</script>
<button type="submit" class="btn btn-primary" onclick="reloadThePage()">Continue</button>

which changes the state of the url but doesn't actually refresh it. Then I have button which refresh the current page, which directly trigger below code in my views.py and gets the code through request.GET['code'] .

Views.py

def instaAuth(request):
    if 'code' in dict(request.GET).keys():

        code = request.GET['code']
        print('the given code is', code)
        core = instaCore()
        user_id, short_lived_access_token = core.code_to_short_access_with_userid(code_string=code)
        print(user_id)
        obj = InstaModel.objects.get(username=request.user)
        obj.instagram_userid = user_id
        obj.short_lived_access_token = short_lived_access_token
        obj.is_active = True
        obj.save()

        print('all data saved!')
        messages.success(request, "Your instagram is connected!")
        return render(request, 'authentication/success.html')

    return render(request, 'authentication/instaAuth.html')

Above code works perfectly fine when I add the code using pushState mehod and refresh the page using button. but when I do the same in my webapp, authorising the app and then clicking continue button, it throughs the KeyError at /instaAuth 'access_token' .

This error usually occurs when someone tries to use the same oauth code more than once. When I looked into the logs I found the error in the same line where I was exchanging the oauth code with the access token. I tried to look into the network tab of the requests, but I am not sure what is missing. I have done similar thing using streamlit and it worked fine, you can check the streamlit app here https://instalogintest.herokuapp.com/

I am stuck at this place, I want to have some logic which either doesn't refresh the page or a way that django knows that this request is coming from Instagram and verify the user using the code.

Problem solved after going through everything for a whole day. Can't believe it was such a small mistake.

I didn't change the redirect uri when I was exchanging the code with access_token .

The problem was I was not printing the error, it always through try and error block, will never do the same again.

Always use this syntax

try
   do_something()
except Exception as e:
   print(e)
   do_something_else()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM