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

Forum Discussion

dotNET_Guy's avatar
dotNET_Guy
Explorer | Level 3
5 years ago
Solved

OAuth authentication for offline application using .NET SDK

Hi Guys,   I've been reading a lot of the Dropbox API documents to get OAuth2 authentication working for my offline Windows application using the .NET SDK. The 'OAuth Guide' was a great starting po...
  • Greg-DB's avatar
    5 years ago

    Use of a redirect URI is optional, and you can still request offline access to get a refresh token back without using a redirect URI. When not using a redirect URI, the user would just need to manually copy/paste the authorization code into the app.

     

    You can do that like this:

     

    First, get the authorization URL, requesting offline access, like:

    var authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(oauthResponseType:OAuthResponseType.Code,
    													   clientId: AppKey,
    													   redirectUri: (String)null,
    													   state: state,
    													   tokenAccessType: TokenAccessType.Offline,
    													   scopeList: null,
    													   includeGrantedScopes: IncludeGrantedScopes.None);

     Open that in the user's browser, such as like in the example:

    System.Diagnostics.Process.Start(authorizeUri.ToString());

    Have the user copy/paste the code into your app. This can be via your UI, but for the sake of example, this is how a console app might do this:

    var code = Console.ReadLine();

    Finish the process by exchanging the authorization code:

    var tokenResult = await DropboxOAuth2Helper.ProcessCodeFlowAsync(code: code, appKey: AppKey, appSecret: AppSecret, redirectUri: null);

    (Note that this is the non-PKCE flow, for server-side apps. For the PKCE flow, for client-side apps, a code challenge/verifier is used instead of the app secret.)

    Then you can make a client using the resulting credentials:

    var client = new DropboxClient(appKey: AppKey,
                          appSecret: AppSecret,
                          oauth2AccessToken: tokenResult.AccessToken,
                          oauth2RefreshToken: tokenResult.RefreshToken,
                          oauth2AccessTokenExpiresAt: tokenResult.ExpiresAt.Value);

    The SDK will automatically perform the refresh process for you when needed when using a client with the necessary credentials like this.

About Discuss Dropbox Developer & API

Node avatar for Discuss Dropbox Developer & API
Make connections with 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!