How to use OAuth 2 - OAuth 2 C# example

↘锁芯ラ 提交于 2019-12-06 20:12:51

问题


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;
}

回答1:


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.



来源:https://stackoverflow.com/questions/14876126/how-to-use-oauth-2-oauth-2-c-sharp-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!