I'm completely confused. I do it according to the documentation,
if I make a link with my hands in my browser and insert the AppiKey, then everything works,
if I try to do it through the code, I get this Invalid redirect_uri message.
It must exactly match one of the redirect URIs you've pre-configured for your app (including the path). In console I added redirect url just like in code private const string LoopbackHost = "http://127.0.0.1:4200/"; what is my problem
// Add an ApiKey (from https://www.dropbox.com/developers/apps) here
private const string ApiKey = "XXX";
// This loopback host is for demo purpose. If this port is not
// available on your machine you need to update this URL with an unused port.
private const string LoopbackHost = "http://127.0.0.1:4200/";
// URL to receive OAuth 2 redirect from Dropbox server.
// You also need to register this redirect URL on https://www.dropbox.com/developers/apps.
private readonly Uri RedirectUri = new Uri(LoopbackHost + "authorize");
// URL to receive access token from JS.
private readonly Uri JSRedirectUri = new Uri(LoopbackHost + "token");
public async void MainRun()
{
var accessToken = await this.GetAccessToken();
}
private async Task<string> GetAccessToken()
{
var accessToken = string.Empty;
if (string.IsNullOrEmpty(accessToken))
{
try
{
Console.WriteLine("Waiting for credentials.");
var state = Guid.NewGuid().ToString("N");
var authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Token, ApiKey, RedirectUri, state: state);
var http = new HttpListener();
http.Prefixes.Add(LoopbackHost);
http.Start();
Console.WriteLine(1);
//Process.Start("C:\\Program Files\\Internet Explorer\\IExplore.exe", authorizeUri.ToString());
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
string url = authorizeUri.ToString().Replace("&", "^&");
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}"));//{ CreateNoWindow = true }
//Console.WriteLine(url);
}
else
{
Process.Start(authorizeUri.ToString());
}
// Handle OAuth redirect and send URL fragment to local server using JS.
await HandleOAuth2Redirect(http);
Console.WriteLine(2);
// Handle redirect from JS and process OAuth response.
var result = await HandleJSRedirect(http);
Console.WriteLine(3);
if (result.State != state)
{
// The state in the response doesn't match the state in the request.
return null;
}
Console.WriteLine("and back...");
accessToken = result.AccessToken;
var uid = result.Uid;
Console.WriteLine("Uid: {0}", uid);
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
return null;
}
}
return accessToken;
}
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();
}
/// <summary>
/// Handle the redirect from JS and process raw redirect URI with fragment to
/// complete the authorization flow.
/// </summary>
/// <param name="http">The http listener.</param>
/// <returns>The <see cref="OAuth2Response"/></returns>
private async Task<OAuth2Response> 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();
}
var redirectUri = new Uri(context.Request.QueryString["url_with_fragment"]);
var result = DropboxOAuth2Helper.ParseTokenFragment(redirectUri);
return result;
}