cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: It must exactly match one of the redirect URIs you've pre-configured for your app (including the

It must exactly match one of the redirect URIs you've pre-configured for your app (including the pat

Shy1
Explorer | Level 3
 

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

 

 

 

12 Replies 12

Здравко
Legendary | Level 20

@Shy1 wrote:
...
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

...


Hi @Shy1,

To be honest, I'm not certain what exactly you're asking for (the question contains response within). I will speculate that you cannot understand difference between terms URI and host (domain name) and that's why you mix them. Your LoopbackHost keeps the host (represented as IP 127.0.0.1 - i.e. loopback IP) as part of URL used as a base for further construction of final redirect URI (here it's URL actually). On registration, complete URL have to be used without possible parameters (anchor isn't used at all). The parameters will be added on redirection and you should process them in your code. 😉 Hope now it's a bit more clear. Take a look here for more info related to URL anatomy (URI can be considered as a URL generalization, but in your case they mean the same thing in fact). 😁 How looks like the redirect URI in your code and is it registered appropriately? 🧐🙂 If you are still not sure, what appears in your browser address bar on redirection (and before that) and after remove parameters is it the same as the registered URI? Is your browser launched with the same URL when you make it by hands and through the code? Why?

Hope this gives direction and clarifies matter.

 

In addition, you can read the comments in your code:


@Shy1 wrote:

...

...
        // 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");
...

...


Does the above advice (need to register this redirect URL on) take proper attention? 🤔

Shy1
Explorer | Level 3

i am just delete anchor and error disapear. but i am still cant redirect to my 127.0.01 i suppose at my configuration pc problem? but at the browser url i have request

Здравко
Legendary | Level 20

@Shy1 wrote:

i am just delete anchor and error disapear. ...


@Shy1, Hmm.. 🤔 How have you deleted something nonexistent? I can see some anchor in your code responsible for redirect URI construction! I'm not sure you have some "configuration pc problem", but most probably you are mixing anchor and resource path. 🙂

 


@Shy1 wrote:

... but at the browser url i have request


Probably you mean the "redirect_uri" parameter there. Is the parameter value registered in the form it appears without your "correction"? 😉

Let me guess what's there. At the beginning you have a constant:

private const string LoopbackHost = "http://127.0.0.1:4200/";

The 'LoopbackHost's value is clear, as you noted it's "http://127.0.0.1:4200/". Let's see the next value:

        // You also need to register this redirect URL on https://www.dropbox.com/developers/apps.
        private readonly Uri RedirectUri = new Uri(LoopbackHost + "authorize");

 Now we have previous value concatenated with "authorize" (i.e. 'RedirectUri's value is "http://127.0.0.1:4200/authorize"). 😉 Is that what appear? And as noted (in the comment), had you registered it? 🧐

About how your PC handle it, take a look here:

while (context.Request.Url.AbsolutePath != RedirectUri.AbsolutePath)

 You code waits for the same 'RedirectUri's value and ignore everything else (keeps looping) until appearing. Can this even happen in your case? (your PC receives this value "corrected" by your hand!!!)

Shy2
New member | Level 2

yes, I have it registered, because I can read. I get a response from the correct link, but my bowser gets the following error and the code exits without any exceptions, it just dies out. if I go through the debugger then I get to the HandleJSRedirect and everything ends there and if not through the debugger, then it ends on the process call Process.Start(); I do not understand anything

ERR_CONNECTION_REFUSED link what i have but i cant work with her and link down

http://127.0.0.1:4200/authorize#access_token=sl.BRajueVNx4yGT0QUk5Hrf5Yf82XUgRCceBpdV_IhTN86MLecaUBG...

maybe it's how i run it?

 

 

static void Main(string[] args)
        {
            Program program = new Program();
            var task = Task.Run(program.MainRun);
            task.Wait();
        }

        public async void MainRun()
        {
            var accessToken = await this.GetAccessToken();
        }

 

 

Здравко
Legendary | Level 20

@Shy2 wrote:

... if I go through the debugger then I get to the HandleJSRedirect and everything ends there ...


On the previous call (call to 'HandleOAuth2Redirect') you have to send back to the browser a 'index.html' file (actually almost entire javascript content), responsible for additional redirection that seems missing. Are you sure correct content got the way? 🧐 You usually can debug/check this using the embedded browser tools (trace the communication). After the second redirect your browser should get redirected to something like:

http://127.0.0.1:4200/token?url_with_fragment=...

Does this happen?

Shy1
Explorer | Level 3

if I take an example from github, and just change the AppKey and AppSecret and set my port, then everything goes fine and I get the following error: an error occurred while sending the request at the moment Exchanging code for token 

Здравко
Legendary | Level 20

@Shy1 wrote:

...

...
            // 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);
            }
...

Ok, let's take a look on above part of your code. Do you understand what exactly this code does and is the file "index.html" available?

Shy1
Explorer | Level 3

if I take an example from github, and just change the AppKey and AppSecret and set my port, then everything goes fine and I get the following error: an error occurred while sending the request at the moment Exchanging code for token 

index.html at the project

 

<html>
<script type="text/javascript">
    function redirect() {
        // Append fragment as query string so that server can receive it.
        document.location.href = "/token?url_with_fragment=" + encodeURIComponent(document.location.href);
    }
</script>
<body onload="redirect()"/>
</html>

at this moment

var tokenResult = await DropboxOAuth2Helper.ProcessCodeFlowAsync(redirectUri, ApiKey, ApiSecret, RedirectUri.ToString(), state);

 

Здравко
Legendary | Level 20

@Shy1 wrote:

... and I get the following error: an error occurred while sending the request at the moment Exchanging code for token ...

Are you sure you keep talking for the same thing? 🤔 I cannot find any place in your source code where you receive a authorization code (you're receiving access token only). In this context what actually you're trying to exchange??? 🧐

Need more support?