Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OAuth 2 - OAuth 2 C# example

I have to figure out how to use OAuth 2 in order to use Deviantart api.

I got the client_id and client_secret part

Here the information they give

Endpoints

The only information you need to authenticate with us using OAuth 2.0 are the client_id and client_secret values for your app, as well as the endpoint shown below.

OAuth 2.0 draft 10:

https://www.deviantart.com/oauth2/draft10/authorize https://www.deviantart.com/oauth2/draft10/token

OAuth 2.0 draft 15:

https://www.deviantart.com/oauth2/draft15/authorize https://www.deviantart.com/oauth2/draft15/token

Placebo call

The first API call relying on OAuth 2.0 authentication is the placebo call. It's useful for checking that an access token is still valid before making a real API call that might be long, like a file upload. You call it with one of the following endpoints (an access token must be provided):

https://www.deviantart.com/api/draft10/placebo https://www.deviantart.com/api/draft15/placebo

You need to use the endpoint that corresponds to the OAuth 2.0 draft you've obtained your token with.

It always returns the following JSON: {status: "success"}

I have searched the web and found this awesome library.

DotNetOpenAuth v4.0.1

http://www.dotnetopenauth.net/

Added it as reference but have no idea what to do next. Even a very small example would be really useful about how to use OAuth 2

using DotNetOpenAuth;
using DotNetOpenAuth.OAuth2;

Here the page where deviantart gives the information

http://www.deviantart.com/developers/oauth2

Ok here what i got so far but not working

public static WebServerClient CreateClient() {
    var desc = GetAuthServerDescription();
    var client = new WebServerClient(desc, clientIdentifier: "myid");
    client.ClientCredentialApplicator = ClientCredentialApplicator.PostParameter("mysecret");
    return client;
}

public static AuthorizationServerDescription GetAuthServerDescription() {
    var authServerDescription = new AuthorizationServerDescription();
    authServerDescription.AuthorizationEndpoint = new Uri(@"https://www.deviantart.com/oauth2/draft15/authorize");
    authServerDescription.TokenEndpoint = new Uri(@"https://www.deviantart.com/oauth2/draft15/token");
    authServerDescription.ProtocolVersion = ProtocolVersion.V20;
    return authServerDescription;
}
like image 654
MonsterMMORPG Avatar asked Feb 14 '13 13:02

MonsterMMORPG


People also ask

What is OAuth 2.0 and how it works?

The OAuth 2.0 authorization framework is a protocol that allows a user to grant a third-party web site or application access to the user's protected resources, without necessarily revealing their long-term credentials or even their identity.


1 Answers

Easiest thing to do now is get Visual Studio 2013 and create a new ASP.NET Web Application choosing "Individual User Accounts" as your authentication type. There's a working OAuth 2 implementation out of the box in there (configured at App_Start\Startup.Auth.cs) which you can slice out and then adapt to your needs.

like image 120
Iain Galloway Avatar answered Oct 24 '22 03:10

Iain Galloway