7digital / SevenDigital.Api.Wrapper

A fluent c# wrapper for the 7digital API
MIT License
16 stars 29 forks source link

Provide full example of how to use OAuth with .NET Wrapper #234

Closed apacha closed 6 years ago

apacha commented 8 years ago

It would be great to provide a complete example of how to use the .NET-Wrapper to perform an OAuth authentication (that seems to be supported but without any samples or descriptions).

I've tried something like

var requestToken = await api.Create<OAuthRequestToken>().Please(); var accessToken = await api.Create<OAuthAccessToken>().WithParameter("oauth_token", requestToken.Token).Please();

but it just keeps throwing "Invalid Signature" exceptions.

I've also read the Guide but so far I wasn't able to obtain a access-token.

danhaller commented 8 years ago

Hey @apacha,

The OAuth endpoints should "just work" in the same way as unsigned endpoints. The wrapper handles the OAuth authentication automatically by looking for the OAuthSigned attribute.

I'l leave this open so we can make this clearer in the documentation - feel free to PR.

apacha commented 8 years ago

I was hoping to find a working example that shows how to do the following things, required to work as authenticated user:

I partially used Google OAuth, DotNetOpenAuth and some other sources, such as a demo how to do OAuth with Spotify to get it running, but it took me hours and it would be great if you could provide a small sample where the user can basically enter his credentials and the library will handle the rest, e.g. ISevenDigitalOAuthProvider sevenDigitalOAuthProvider = new SevenDigitalOAuthProvider(); IOAuthCredentials oauthCredentials = new AppSettingsCredentials(); OAuthAccessToken accessToken = await sevenDigitalOAuthProvider.Authenticate(oauthCredentials);.

gregsochanik commented 8 years ago

Hi @apacha

Just to fill you in, this library is provided to make calls to our api easily with a dotnet app, so a lot of the inner workings/api endpoints are abstracted away. There are some examples in the /examples folder.

To obtain a request token:

var api = new ApiFactory(new ApiUri(), new AppSettingsCredentials());
var requestToken = await api.Create<OAuthRequestToken>()
    .Please();

ApiUri is a basic object that inherits from IApiUri containing the 7digital api url. e.g. https://github.com/7digital/SevenDigital.Api.Wrapper/blob/master/examples/ExampleUsage/ExampleUsage/ApiUri.cs

AppSettingsCredentials is an object inheriting from IOAuthCredentials pointing at somewhere that has access to your api key / secret. e.g:

https://github.com/7digital/SevenDigital.Api.Wrapper/blob/master/examples/ExampleUsage/ExampleUsage/AppSettingsCredentials.cs

Access granting web-page

Details of the web-page where access can be granted is here: http://developer.7digital.com/resources/133

Replace YOUR_KEY_HERE with your api key, and oauth_token / oauth_secret with the ones returned by the call to the request/token endpoint above.

Finally:

Exchange authorised request token for access token:

var accessTokenTokenApi = await api.Create<OAuthAccessToken>()
    .ForUser(requestToken.Token, requestToken.Secret)
    .Please();

I'll try and get this added to the readme, you should be able to find more info in the 7digital Api documentation.

There is an api requesttoken authentication endpoint, which requires a Premium api account to take advantage of.

Hope this helps! Greg

apacha commented 8 years ago

This definitely would have helped a lot if it was included in one of the examples or the documentation or the .NET wrapper! Especially the third part was a pain to implement and obviously can be solved with your API elegantly. Please create another sample which contains exactly these steps, or at least has placeholders for programmers to fill in. (e.g for part two, where a simple HTTP-call has to be made).

The existing samples are just too simple and do not even cover this basic use-case. As for the fluent syntax-API is it really obfuscated, what can and what can't be created, and which parameters are required and which are optional. Especially the call ForUser seems to have multiple undocumented meanings (it takes a request token for creating an access-token and takes an access-token for creating a Locker object).

gregsochanik commented 8 years ago

Thanks for your comments, they have been noted. We'll try and add some better examples soon, but in the mean time free to submit a pull request.