Ender Aric
Ender Aric

Reputation: 257

Asp.Net file upload to dropbox via api?

I am trying to use HighLabo for dropbox communications. But it is giving an error. Here is my code

    private const string App_key = "my_app_key";
    private const string App_secret = "my_app_secret";
    OAuthClient ocl = null;
    HigLabo.Net.AuthorizeInfo ai = null;

    public void UploadFile(byte[] content, string filename, string target)
    {

        ocl = DropboxClient.CreateOAuthClient(App_key, App_secret);
        ai = ocl.GetAuthorizeInfo();
        string RequestToken = ai.RequestToken;
        string RequestTokenSecret = ai.RequestTokenSecret;
        string redirect_url = ai.AuthorizeUrl;
        AccessTokenInfo t = ocl.GetAccessToken(RequestToken, RequestTokenSecret);
        string Token = t.Token;
        string TokenSecret = t.TokenSecret;

        DropboxClient cl = new DropboxClient(App_key, App_secret, Token, TokenSecret);

        HigLabo.Net.Dropbox.UploadFileCommand ul = new HigLabo.Net.Dropbox.UploadFileCommand();
        ul.Root = RootFolder.Sandbox;
        ul.FolderPath = target;
        ul.FileName = filename;
        ul.LoadFileData(content);

        Metadata md = cl.UploadFile(ul);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //string filename = Path.GetFullPath(FileUpload1.FileBytes);
        //byte[] bytes = System.IO.File.ReadAllBytes(filename); 
        UploadFile(FileUpload1.FileBytes, "sundas.jpg", "/Apps/synch/");   
    }

I am getting value can not be null error on

ai = ocl.GetAuthorizeInfo();

line. Does anyone know this problem?

Also i tried using Nemiro and Dropnet libraries. In Nemiro i can use with winforms. But neither nemiro nor dropnet is hard for webforms. I have to fix this problem.

Upvotes: 0

Views: 1344

Answers (2)

Ender Aric
Ender Aric

Reputation: 257

I fixed my problem with Nemiro.OAuth. The following example is very helpful.

https://github.com/alekseynemiro/nemiro.oauth.dll/tree/master/examples/DropBoxWebForms

Upvotes: 0

Aleksey Nemiro
Aleksey Nemiro

Reputation: 86

Nemiro.OAuth was created in the first place for ASP.NET (WebForms, MVC).

The following link you can find example for Dropbox and WebForms: https://github.com/alekseynemiro/nemiro.oauth.dll/tree/master/examples/DropBoxWebForms

After the authorization of a user, you should save an access token.

Typically, save access token to database. The examples use the Session. But the Session is not a good place to store the access token.

If you save the access token to database, or at least a text file, you can use it to query, without re-authorization.

I think you have the same problems with HighLabo. You need to save an access token after authentication, and use it for all requests to the API.

Upvotes: 1

Related Questions