OAuth – Is open standard for access delegation.
It is used to provides “secured delegated access”
In this example, I will show how Oauth 2.0 works with Foursquare.com app authentication as example.
Sample Oauth 2.0 flow diagram will explain how things works in pictorial way.
Oauth 2.0 Flow specific to Foursquare.
As shown on Foursquare.com (Step by step example)
Your foursquare app will redirect users to Foursquare.com
https://foursquare.com/oauth2/authenticate ?client_id=YOUR_CLIENT_ID &response_type=code &redirect_uri=YOUR_REGISTERED_REDIRECT_URI
If the user allows access to your foursquare app, they will be redirected back to your app with an authorization code.
https://YOUR_REGISTERED_REDIRECT_URI/?code=CODE
Your foursquare app should exchange the authorization code it got in step 2 for an access token. Make a request for
https://foursquare.com/oauth2/access_token ?client_id=YOUR_CLIENT_ID &client_secret=YOUR_CLIENT_SECRET &grant_type=authorization_code &redirect_uri=YOUR_REGISTERED_REDIRECT_URI &code=CODE
The response will be JSON
{ access_token: ACCESS_TOKEN }
Save this access token for this user in your database.