We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.
Forum Discussion
Ivan_
3 years agoHelpful | Level 6
How to programmatically get the Authorization code without requiring the user approval in OAuth 2.0
Hi,
I need to get approve code programmatically without this form via c# code
this is my code
How it should be modified in order to get approve code automatically without us...
- 3 years ago
Ivan_ wrote:...
The problem is that this code does not work in my Application. It stops at Proce/ssCodeFlowAsync method and after that the eternal loading of response begins. Sometimes eternal loading begins at HandleJSRedirect and HandleOAuth2Redirect methods, I do not know what causes such behavior.
...
Ivan_, the code is for one time use!!! Once you get your credentials (access token and refresh token) the code isn't valid anymore! You should keep received credentials (primary the refresh token) and reuse later when needed to construct client object. Why after the authentication (you have credentials already) are you calling still handlers designate for initial authentication? 🤔 I cannot see a reason.
Ivan_
Helpful | Level 6
Hi Здравко
Thank you for provided information
I faced with following problem
In console application everything works fine and token successfully generated
Here I attach body of the await methods
Some important notes :
RedirectUri variable is local host
redirectUri is bariable with value that DropBox returns to me with approve code
The rest part of the code is same to the example that is provided in documentation
The problem is that this code does not work in my Application. It stops at Proce/ssCodeFlowAsync method and after that the eternal loading of response begins. Sometimes eternal loading begins at HandleJSRedirect and HandleOAuth2Redirect methods, I do not know what causes such behavior.
I want to mention that in Console Application everything works great, without any troubles, but when I execute same code in my project app - eternal loading always starts, it looks like some troubles with threads, but I do not know what is wrong.
Here is my code in text
var http = new HttpListener();
http.Prefixes.Add(LoopbackHost);
http.Start();
var state = Guid.NewGuid().ToString("N");
var authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Code, ApiKey,
RedirectUri, state: state, tokenAccessType: TokenAccessType.Offline, scopeList: scopeList, includeGrantedScopes: includeGrantedScopes);
System.Diagnostics.Process.Start(authorizeUri.ToString());
// Handle OAuth redirect and send URL fragment to local server using JS.
await HandleOAuth2Redirect(http);
// Handle redirect from JS and process OAuth response.
var redirectUri = await HandleJSRedirect(http);
var tokenResult = await DropboxOAuth2Helper.ProcessCodeFlowAsync(redirectUri, ApiKey, ApiSecret, RedirectUri.ToString(), state);
Console.WriteLine("Finished Exchanging Code for Token");
string Access = tokenResult.AccessToken;
-------------------------------------------------------------------------------
private async Task<Uri> HandleJSRedirect(HttpListener http)
{
var context = await http.GetContextAsync();
// We only care about request to TokenRedirectUri endpoint.
//while (context.Request.Url.AbsolutePath != JSRedirectUri.AbsolutePath)
//{
// context = await http.GetContextAsync();
//}
Uri redirectUri = new Uri(context.Request.UrlReferrer.ToString());
return redirectUri;
}
private async Task HandleOAuth2Redirect(HttpListener http)
{
var context = await http.GetContextAsync();
// We only care about request to RedirectUri endpoint.
while (context.Request.Url.AbsolutePath != RedirectUri.AbsolutePath)
{
context = await http.GetContextAsync();
}
//context.Response.ContentType = "text/html";
//Respond with a page which runs JS and sends URL fragment as query string
//to TokenRedirectUri.
//using (var file = File.OpenRead("index.html"))
//{
// file.CopyTo(context.Response.OutputStream);
//}
context.Response.OutputStream.Close();
}
Здравко
3 years agoLegendary | Level 20
Ivan_ wrote:...
The problem is that this code does not work in my Application. It stops at Proce/ssCodeFlowAsync method and after that the eternal loading of response begins. Sometimes eternal loading begins at HandleJSRedirect and HandleOAuth2Redirect methods, I do not know what causes such behavior.
...
Ivan_, the code is for one time use!!! Once you get your credentials (access token and refresh token) the code isn't valid anymore! You should keep received credentials (primary the refresh token) and reuse later when needed to construct client object. Why after the authentication (you have credentials already) are you calling still handlers designate for initial authentication? 🤔 I cannot see a reason.
- Ivan_3 years agoHelpful | Level 6
Understood, thanks
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,877 PostsLatest Activity: 12 months agoIf you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!