Code Monkey home page Code Monkey logo

chq.oauth's Introduction

Chq.OAuth

Introduction

Chq.OAuth is a simple C# OAuth library for creating OAuth consumers in Windows 8 Metro applications. This is an early stage project so things will probably change :)

You can grab the latest version on NuGet: https://nuget.org/packages/Chq.OAuth.dll

Usage

1: Create an OAuthContext object, this holds a whole bunch of informaion about the service that you're communicating with.

var context =  new OAuthContext(ConsumerKey, ConsumerSecret, RequestUrl, AuthorizeUrl, AccessUrl, CallbackUrl);

2: Create a client

var client =  new Client(context);

3: Request a temporary token (The RequestToken) from the OAuth provider

String requestTokenResponse = await client.MakeRequest("GET")
                    .ForRequestToken()
                    .WithParameters( new { scope = "email"} ) //Optional, changes depending on provider
                    .Sign()
                    .ExecuteRequest();
                    
client.RequestToken = TokenContainer.Parse(requestTokenResponse);

4: Authorize with the temporary RequestToken

Uri authorizationUri = client.GetAuthorizationUri();
                    
//Authorize the temporary token using the authorizationUri
//One option is to use the supplied WebAuthenticationBroker
WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, authorizationUri, client.Context.CallbackUri);

//The verification code could be returned in the response
//i.e. Parse it out of WebAuthenticationResult.ResponseData.ToString();
//Or it could be displayed to the user and they will have to enter it into your application manually
String verificationCode = ... 

5: Exchange the temporary token for an access token

String accessTokenResponse = await client.MakeRequest("GET")
                    .ForAccessToken(client.RequestToken.Token, verificationCode)
                    .Sign(client.RequestToken.Secret)
                    .ExecuteRequest();
                    
client.AccessToken = TokenContainer.Parse(accessTokenResponse);

6: Yay, you're done! Now you can access protected resources.

String getResponse = await client.MakeRequest("GET")
                  .ForResource(client.AccessToken.Token, protectedResourceUri)
                  .WithParameters(new { param = "value" }) //it's important to add the prameters after the resource, internal paramerters reset on ForResource calls
                  .Sign(client.AccessToken.Secret)
                  .ExecuteRequest();
                  
String postResponse = await client.MakeRequest("POST")
                  .WithData(data)
                  .ForResource(client.AccessToken.Token, protectedResourceUri)
                  .Sign(client.AccessToken.Secret)
                  .ExecuteRequest();

String postResponse = await client.MakeRequest("POST")
                  .WithFormEncodedData(new {name = "Peter"}) //this will be sent as a key/value in the request body an is included in the OAuth signature
                  .ForResource(client.AccessToken.Token, protectedResourceUri)
                  .Sign(client.AccessToken.Secret)
                  .ExecuteRequest();

7: Fork it, have fun, create a wrapper for your favourite web applications

chq.oauth's People

Contributors

coolya avatar myquay avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.