Kelvin Kelvin - 4 months ago 13
Python Question

How to detect url callback request

I have a locally-run app that makes API calls to a website (

). This involves setting some OAuth credentials; one step along the way requires extracting a key from a callback url that the server directs the browser to. So what I currently have the user do is:


  1. Open an authorization link in a browser, which prompts them to authorize the OAuth application on the website

  2. Click through the authorization page on the website (“Yes, I allow xxxxx app to access certain info associated with my account”)

  3. Clicking
    Authorize app
    makes a request to the localhost which includes a parameter in the url. Meaning that tumblr will redirect the browser to the page
    http://localhost/?oauth_token={TOKEN}&oauth_verifier={VERIFIER}#_=_
    . I assume that causes a request to be made to the local machine when it does that.

  4. The user is expected to isolate the key parameter in the url from the browser’s navigation bar, and paste it in the application.



So is there any way I can bypass steps 3 and 4 and simply have the app pick up the callback request instead of expecting the user to copy and paste it from the browser? I’m afraid I don’t know much about how to handle network requests in python.

To be clear, what I need to do is get the
{VERIFIER}
string.

Answer

okay first thing first, for http requests, a good python module is requests

http://docs.python-requests.org/en/master/

Then, your app gives a callback address to tumblr so that tumblr can tell to your app client info, or login error.

Now, your point 3 isn't clear. "Clicking authorize app makes a request to localhost"

Actually clicking "authorize app" for the user makes a request to tumblr saying he accepts. Then tumblr makes a request to your callback url passing the infos.

The callback url should probably be your server address, there you must have a script listening for tumblr, which will give you your special parameter to call their api...

In addition :

So when the users click "authorize app" there is a request to tumblr, which redirects the user to the callback url (adding oauth token and verifier).

Now, obviously, you can't ask for every user to have an http server running on their computer.

So you must set the callback url to your server.

So if you set it to "myserver.com/tumblr" for instance, the user will get redirected to your webpage, and you'll get on your server, and for that user session, the oauths token and verifier.

and...

Assuming your app is client only I'd say there are two options.

Either have your users enter manually their API keys.

Or either embed a webserver into your app.

In the case of the embedded webserver, I'd suggest flask for its simplicity. Simply have your webserver listen on a given port and set the callback url to that server:port.

This way you'll get the client tokens directly.