David-Desmaisons / DiscogsClient

Discogs API C# Client
MIT License
41 stars 16 forks source link

Authorize new user #4

Closed traponto closed 6 years ago

traponto commented 6 years ago

I'm having trouble activating the authorize procedure inside my WinForm application, it seems that the 'GetToken' function is not invoked and the program goes in loop in the 'Authorize' procedure. I took the same instructions in the sample application 'DiscogsAuthenticationConsole' where instead everything is fine.

David-Desmaisons commented 6 years ago

Well, It is very hard to diagnostic anything from what you describe. I would say since DiscogsAuthenticationConsole is working try to simplify your code to make it more and more similar to the sample application till it works.

FrancisChung commented 6 years ago

Sounds like you need to synchronize your UI thread context.

https://stackoverflow.com/questions/19028374/accessing-ui-controls-in-task-run-with-async-await-on-winforms

David-Desmaisons commented 6 years ago

@FrancisChung DiscogsClent is (and needs to be) agnostic in term of application context (WPF, or WinForm or console, or ASP etc...). So it is the caller responsability to make sure that the API is called on the "context" context.

traponto commented 6 years ago

I am relatively new to c #. In my WinForm application I used the same code I found in your console application example:

var oAuthConsumerInformation = new OAuthConsumerInformation("X", "Y");

var discogsClient = new DiscogsAuthentifierClient(oAuthConsumerInformation);

var aouth = discogsClient.Authorize(s => Task.FromResult(GetToken(s))).Result;

In the console application the GetToken function is called, instead the Authorize method seems that never come back in the WinForm App.

I did a debugging session to see what happens inside the Authorize method and I found that in the RestSharpHelper:

public async Task GetTokenInformationFromRequest(IRestClient client, string relativeUrl)

{

var request = new RestRequest(relativeUrl, Method.POST);

var response = await client.ExecuteTaskAsync(request);

return !CheckResponse(response) ? null : GetTokenInformationFromBodyResponse(response);

}

...the ExecuteTaskAsync method never end when executed inside the WinForm App.

On Sun, Feb 25, 2018 at 11:18 AM, David Desmaisons <notifications@github.com

wrote:

Well, It is very hard to diagnostic anything from what you describe. I would say since DiscogsAuthenticationConsole is working try to simplify your code to make it more and more similar to the sample application till it works.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/David-Desmaisons/DiscogsClient/issues/4#issuecomment-368298063, or mute the thread https://github.com/notifications/unsubscribe-auth/AiNgcIKj9aJKzradywN3cufd4JdDTo1eks5tYTN4gaJpZM4SSGnh .

traponto commented 6 years ago

Thank you for your answers and for the suggestions you gave me. I took a look at the stackoverflow link (as FrancisChung suggested) and I think I fall into the "deadlock" case described there by "Daniel". I tried to insert the 'Authorize' procedure in a separate thread and I verified that it works. However, I would like to be able to manage the Key (provided by DiscoGS) and tokens (provided by DiscoGSClient) within my WinForm application, so any suggestions to do this are welcome.

FrancisChung commented 6 years ago

@David-Desmaisons My comments were meant to be directed at Traponto's code, not yours. Apologies for the miscommunication. And you're absolutely right about APIs needing to be context neutral.