Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

Littlepage's avatar
Littlepage
New member | Level 2
3 months ago

Can't get Oauth to work... please help

Question or Issue

I'm a little frustrated with the poor examples of dropbox... :-( I'm trying to start with Oauth, I'm trying to build a c# console app that will run for hours, since the tokens expire fast, I tried to get oauth integrated, but at step 1 it dies.. my code starts as follows:

const string AppKey = "XXXXXXX"; // Replace with your App Key
const string AppSecret = "XXXXXXX"; // Replace with your App Secret
const string RedirectUri = "http://localhost:8080/"; // Replace with your Redirect URI

// Generate a unique state for security
string oauth2State = Guid.NewGuid().ToString("N");

// Get the authorization URI
Uri authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(
    OAuthResponseType.Code, 
    clientId: AppKey,
    new Uri(RedirectUri),
    state: oauth2State,
    tokenAccessType: TokenAccessType.Offline 
    
);

BUT as soon as my browser opens, I get an ugly error as follows:

I did check the url and the client_id is there... (of course is not XXXXXXX)

 https://www.dropbox.com/oauth2/authorize?response_type=code&client_id=XXXXXXXXXX&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&state=65b4556d0f1f4639ac42b6cdfce08fb7&token_access_type=offline

I understand my App Key is my client Id, thank you for any help you may bring.

4 Replies

  • DB-Des's avatar
    DB-Des
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    3 months ago

    Hi Littlepage​ 

    Thank you for that additional information.

    I am still unable to replicate the error you originally reported: Missing client_id.

    The full code you provided works without issues on my end.

    Are you able to share the full authorization URL that your program is generating? Since you are logging it: Console.WriteLine("authorizeUri: " + authorizeUri.ToString());

  • Littlepage's avatar
    Littlepage
    New member | Level 2
    3 months ago

    The problem starts on the way we call the new browser window to open... here's the correct code:

    // Corrected Process.Start usage

    var processStartInfo = new ProcessStartInfo
    {
        FileName = authorizeUri.ToString(),
        UseShellExecute = true
    };
    Process.Start(processStartInfo);

  • Littlepage's avatar
    Littlepage
    New member | Level 2
    3 months ago

    Here's my complete code.. and I tried and it doesn't work

    namespace LittlePageUpload3
    {
        using Dropbox.Api;
        using System;
        using System.Diagnostics; // For Process.Start
        using System.Runtime.InteropServices;
        using System.Threading.Tasks;

        public partial class Program
        {
            static async Task Main(string[] args)
            {
                const string AppKey = "xtva8nnzc76bn1g"; // Replace with your App Key
                const string AppSecret = "XXXX"; // Replace with your App Secret
                const string RedirectUri = "http://localhost:8080/"; // Replace with your Redirect URI

                // Generate a unique state for security
                string oauth2State = Guid.NewGuid().ToString("N");

                // Get the authorization URI
                Uri authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(
                    OAuthResponseType.Code, 
                    clientId: AppKey,
                    new Uri(RedirectUri),
                    state: oauth2State,
                    tokenAccessType: TokenAccessType.Offline 
                    
                );

                // Open the URI in the user's default browser
                Console.WriteLine("Opening Dropbox authorization in your browser...");

                
                Console.WriteLine("authorizeUri: " + authorizeUri.ToString());
                Process.Start(new ProcessStartInfo("cmd", $"/c start {authorizeUri}") { CreateNoWindow = true });
                

                Console.WriteLine("Please authorize the app in your browser and copy the code from the redirect URL.");
                Console.Write("Enter the authorization code: ");
                string code = Console.ReadLine();

                // Exchange the authorization code for access and refresh tokens
                var tokenResult = await DropboxOAuth2Helper.ProcessCodeFlowAsync(
                    code: code,
                    appKey: AppKey,
                    appSecret: AppSecret,
                    redirectUri: RedirectUri
                );

                // Store tokenResult.AccessToken and tokenResult.RefreshToken securely
                Console.WriteLine($"Access Token: {tokenResult.AccessToken}");
                Console.WriteLine($"Refresh Token: {tokenResult.RefreshToken}");

            }
        }
    }

    My control panel looks like this:

     

  • DB-Des's avatar
    DB-Des
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    3 months ago

    Hi Littlepage​ 

    Thanks for sharing the code snippet. I tested it on my end and was able to successfully generate a valid authorization URL. I’d recommend double-checking your environment variables and ensuring that the correct values are being passed to their corresponding variables. That should help narrow down what might be happening.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If 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, Facebook or Instagram.

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!