Tuesday, October 20, 2015

Python OAuth implementation it's not that hard!

Did you know about OAuth ? I will explain about OAuth from some resources,

Wikipedia: http://en.wikipedia.org/wiki/OAuth
OAuth is an open standard for authorization. OAuth provides client applications a 'secure delegated access' to server resources on behalf of a resource owner. It specifies a process for resource owners to authorize third-party access to their server resources without sharing their credentials. Designed specifically to work with Hypertext Transfer Protocol (HTTP), OAuth essentially allows access tokens to be issued to third-party clients by an authorization server, with the approval of the resource owner, or end-user. The client then uses the access token to access the protected resources hosted by the resource server. OAuth is commonly used as a way for web surfers to log into third party web sites using their Microsoft, Google, Facebook or Twitter accounts, without worrying about their access credentials being compromised.

If you want to know more about OAuth standard you can refer to http://oauth.net/

In this post I will use Python version 3.4,

Something you need to know about OAouth implementation:
  1. Know about REST API from service provider like twitter, facebook, google, tumblr, yahoo etc. They have different method when request using oauth. some provider using POST only or GET only or both.
  2. Know about OAuth standard at least you have read it
  3. Know about term which is used by OAuth like oauth_callback, oauth_nonce, oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret, oauth_timestamp, oauth_signature, oauth_signature_method and oauth_version
  4. that's it
OAuth using CONSUMER_KEY and CONSUMER_SECRET to generate token, then this token will be use to access API from service provider.
I will use twitter rest api for this implementation, but this will be useable for common OAuth standard :-) don't worry. Before we start you must create application on twitter follow this twitter app registration.

1. If you already have account you can sign in, but if you don't have you need register to twitter

2. After sign in, you will get create application button if you don't have any application. Then just create new app


3. Fill your application name, description, website and callback. Callback for what? callback is url that will call after user success and granted your application. For now we don't really care about this we just want to create oauth impletentation to get token and token secret


4. After finish your app creation, then scooll down on your app detail. You will get some information about url for request token, authorization, access token.


5. Go to tab Keys and Access Tokens and The important thing is you need to retrieve CONSUMER_KEY and CONSUMER_SECRET from your app. Sorry I don't want show my consumer secret :D.


Ok then lets we start, create implementation of OAuth is not hard the only we need is follow the rule.
I already create basic code, using twitter api for testing my code implementation you can grab it from my github repository here https://github.com/amru-rosyada/pyTwitterAPI.

Thats the basic code, but I will add more for retrieve or post status. The basic is enough if you want to implement other request. follow this twitter rest api.

Regards,

No comments:

Post a Comment