Learning Python Network Programming

(Sean Pound) #1
Chapter 3
if __name__ == '__main__':
auth_obj = init_auth()

Remember that consumer_secret and access_secret act as the password to your
Twitter account, so in a production app they should be loaded from a secure external
location instead of being hard-coded into the source code.


In the aforementioned code, we create the OAuth1 authentication instance,
auth_obj, in the init_auth() function by using our access credentials. We
pass this to Requests whenever we need to make an HTTP request, and through
it Requests handles the authentication. You can see an example of this in the
verify_credentials() function.


In the verify_credentials() function, we test whether Twitter recognizes our
credentials. The URL that we're using here is an endpoint that Twitter provides
purely for testing whether our credentials are valid. It returns an HTTP 200 status
code if they are valid or a 401 status code if not.


Now, let's run twitter_worldclock.py and if we've registered our application
and filled out the tokens and secrets properly, then we should see Validated
credentials OK. Now that the authentication is working, the basic flow of our
program will be, as shown in the following diagram:

Free download pdf