cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements
Update: Find information on Dropbox support during COVID-19 here
Close
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I upload a file to my dropbox apps folder in dropbox in objective c?

Chez B. New member | Level 1
New member | Level 1

Currently nothing happens when I run my code.

This is how I am setting the up client. I did not put the client set up in my viewDidLoad method because this class will never be shown it just handles server side actions.

(instancetype)initWithClient  
{

  self = [super init];

  if (self) {

    self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];

    self.restClient.delegate = self;

      }

  return self;  
}  



This is how I make the API call to upload

(void)uploadToDropBox:(UIImage img  
{

  NSLog(@"Entered the DB upload method");


  NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  
                                                             NSUserDomainMask,  
                                                             YES);


  NSString *documentDirectory = [directories firstObject];

  NSString *imagePath = [documentDirectory stringByAppendingPathComponent:@"cardImage"];

  NSString *destDir = @"Apps/BirthdayScrapbook";

  NSLog(@"Just about to execute the upload method");

  NSData *imageData = UIImagePNGRepresentation(img);

  [imageData writeToFile:imagePath atomically: YES];

  [self.restClient uploadFile:@"cardImage" toPath:destDir withParentRev:nil fromPath:imagePath];

  NSLog(@"Executed the DB  upload method");

  [self sendBDayCard];  
}  



I think I am going about it wrong. I know what my access token is, but I don't know what to do with it nor where to put it. There is a client init method that takes in the userID, but I don't know where to find my accounts userID. Can someone give some direction with this?

17 Replies 17

Re: How do I upload a file to my dropbox apps folder in dropbox in objective c?

Super User II
Super User II

Moved to the API forum.

Re: How do I upload a file to my dropbox apps folder in dropbox in objective c?

Dropboxer
Dropboxer

There are a few things to note here:

 

1) Did you implement the callback methods?

 


- (void)restClient:(DBRestClientclient uploadedFile:(NSStringdestPath from:(NSStringsrcPath metadata:(DBMetadatametadata;  
- (void)restClient:(DBRestClientclient uploadFileFailedWithError:(NSErrorerror;



 That's how you receive the requested information back.

2) You don't need to explicitly specify your app folder as "Apps/BirthdayScrapbook". App folder apps are implicitly rooted in their app folder, so you can just use "/".

3) Normally with the iOS or OS X Core SDK, you don't need to supply the access token manually. It's programmatically retrieved using the built-in app authorization flow, as shown in the tutorial:

https://www.dropbox.com/developers/core/start/ios

If you do have an access token you want to set manually though you can use this method on DBSession :


- (void)updateAccessToken:(NSString token accessTokenSecret:(NSString secret forUserId:(NSString userId;



The userId is the integer user ID for your account, e.g., as seen in DBSession.userIds , or as returned by the /account/info endpoint, which is loadAccountInfo ( loadedAccountInfo and loadAccountInfoFailedWithError callbacks) in the Core SDK.

Re: How do I upload a file to my dropbox apps folder in dropbox in objective c?

Chez B. New member | Level 1
New member | Level 1

I have the call back methods implemented in the same class like so.

(void)restClient:(DBRestClient client uploadedFile:(NSString destPath from:(NSString srcPath metadata:(DBMetadata metadata {
    NSLog(@"File uploaded successfully to path: %@", metadata.path);
}

 (void)restClient:(DBRestClient client uploadFileFailedWithError:(NSError error {
    NSLog(@"File upload failed with error: %@", error);  
}  


Though I have not called them anywhere in the program. I suppose that may not be necessary.

I tried to add the updateAcessToken method inside my didFinishLaunching method like this. I have my actual app key and app secret in my actual code. I just omitted them for security.

DBSession *dbSession = [[DBSession alloc]
                        initWithAppKey:@"db-myAppKey"
                        appSecret:@"myAppSecret"
                        root:kDBRootAppFolder]; // either kDBRootAppFolder or kDBRootDropbox

 [dbSession updateAccessToken:@"myAccessToken" accessTokenSecret:@"MyAppsAppSecret" forUserId:[[DBAccountInfo alloc] init].userId];

[DBSession setSharedSession:dbSession];  


Xcode gives me the error:

setObjectForKey: key cannot be nil

userID is nil which makes sense. How do I obtain the user ID that is associated with the account that my app was created on without having to bring up a formal dropbox login view when I run the app? I want to be logged into the account that my app was created on, and obtain the userID all in the back end. I don't want the user of the app to have any knowledge that this is going on.

Re: How do I upload a file to my dropbox apps folder in dropbox in objective c?

Dropboxer
Dropboxer

Thanks. That's correct, you don't have to call the callback methods yourself, they get called by the SDK.

Also, note that the app key passed to initWithAppKey should be just the app key, without the "db-" prefix. The "db-" prefix is only necessary for the URL scheme you register in the plist.

Finally, yes, as you mentioned this error is expected as you're just initializing an empty DBAccount object, so the userId isn't set. 

Normally, you don't need to know the user ID to start with, as the user authorizes the account, and doesn't supply an access token themselves. It is possible to use API/SDK like this with a single account, but it isn't recommended, for a variety of security and technical reasons.

If you do go this route though, you'll need to get the user ID for your account once, just like you got an access token for your account once. You can do this by calling the /account/info endpoint manually once. This blog post covers how to make these calls with OAuth 1 manually if you need.

Re: How do I upload a file to my dropbox apps folder in dropbox in objective c?

Chez B. New member | Level 1
New member | Level 1

I am only using the API/SDK with one user account because I am working on app that is not going to be published. I am interning a Facebook and I am in a program where the focus is to teach us mobile development. This app is a learning experience. I just need an easy way to store videos that can be accessed by whoever we email the dropbox link to. 

Since I am working in objective c how do I call the method /account/info endpoint?

Is there a method in the iOS SDK that makes calling those types of methods possible?

Re: How do I upload a file to my dropbox apps folder in dropbox in objective c?

Dropboxer
Dropboxer

Yes, the loadAccountInfo method makes that API call, but you need to be linked to do that first anyway. 

To get your user ID once, you can make the call once, e.g.:

curl "https://api.dropbox.com/1/account/info?oauth_version=1.0&oauth_signature_method=PLAINTEXT&oauth_nonce=NONCE_HERE&oauth_consumer_key=APP_KEY_HERE&oauth_token=ACCESS_TOKEN_KEY_HERE&oauth_timestamp=CURRENT_TIMESTAMP_HERE&oauth_signature=APP_SECRET_HERE%26ACCESS_TOKEN_SECRET_HERE"

Re: How do I upload a file to my dropbox apps folder in dropbox in objective c?

Chez B. New member | Level 1
New member | Level 1

I ran this though my terminal while in the directory of where my app is located

curl "https://api.dropbox.com/1/account/info?oauth\_version=1.0&oauth\_signature\_method=PLAINTEXT&oauth\_..."

and this happened

<!DOCTYPE html>

<html>

<head><title>Dropbox - 4xx</title>

<link href="//www.dropbox.com/static/css/error.css" rel="stylesheet" type="text/css"/>

<link rel="shortcut icon" href="//www.dropbox.com/static/images/favicon.ico"/>

</head>

<body>

<div class="figure">

<img src="//www.dropbox.com/static/images/psychobox.png" alt="Error: 4xx"/>

</div>

<div id="errorbox">

<h1>Error (4xx)</h1>We can't find the page you're looking for. Check out our <a href="https://www.dropbox.com/help">Help Center</a> and <a href="https://forums.dropbox.com">forums</a> for help, or head back to <a href="https://www.dropbox.com/home">home</a>.

</div>

 

<script type="text/javascript" src="//www.dropbox.com/static/javascript/external/prototype.js"></script>

<script>

message = {"ru": "\u003ch1\u003e\u041e\u0448\u0438\u0431\u043a\u0430 (4xx)\u003c/h1\u003e\u0417\u0430\u043f\u0440\u043e\u0448\u0435\u043d\u043d\u0430\u044f \u0432\u0430\u043c\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430. \u041f\u0435\u0440\u0435\u0439\u0434\u0438\u0442\u0435 \u0432 \u003ca href=\"https://www.dropbox.com/help\"\u003e\u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u044b\u0439 \u0446\u0435\u043d\u0442\u0440\u003c/a\u003e \u0438\u043b\u0438 \u043d\u0430 \u003ca href=\"https://forums.dropbox.com\"\u003e\u0444\u043e\u0440\u0443\u043c\u044b\u003c/a\u003e \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u043c\u043e\u0449\u0438 \u0438\u043b\u0438 \u0432\u0435\u0440\u043d\u0438\u0442\u0435\u0441\u044c \u043d\u0430 \u003ca href=\"https://www.dropbox.com/home\"\u003e\u0434\u043e\u043c\u0430\u0448\u043d\u044e\u044e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443\u003c/a\u003e.", "fr": "\u003ch1\u003eErreur (4xx)\u003c/h1\u003eLa page que vous recherchez est introuvable. Consultez notre \u003ca href=\"https://www.dropbox.com/help\"\u003ecentre d'assistance\u003c/a\u003e et nos \u003ca href=\"https://forums.dropbox.com\"\u003eforums\u003c/a\u003e pour obtenir de l'aide, ou retournez sur la page \u003ca href=\"https://www.dropbox.com/home\"\u003eAccueil\u003c/a\u003e.", "nb_NO": "\u003ch1\u003eFeil (4xx)\u003c/h1\u003eVi finner ikke siden du leter etter. Sjekk ut v\u00e5rt \u003ca href=\"https://www.dropbox.com/help\"\u003ehjelpesenter\u003c/a\u003e og \u003ca href=\"https://forums.dropbox.com\"\u003efora\u003c/a\u003e for hjelp, eller g\u00e5 tilbake til \u003ca href=\"https://www.dropbox.com/home\"\u003ehjemmesiden\u003c/a\u003e.", "zh_TW": "\u003ch1\u003e\u932f\u8aa4 (4xx)\u003c/h1\u003e\u7121\u6cd5\u627e\u5230\u60a8\u60f3\u627e\u7684\u7db2\u9801\u3002\u8acb\u5230\u6211\u5011\u7684\u003ca href=\"https://www.dropbox.com/help\"\u003e\u8aaa\u660e\u4e2d\u5fc3\u003c/a\u003e\u548c\u003ca href=\"https://forums.dropbox.com\"\u003e\u8a0e\u8ad6\u5340\u003c/a\u003e\u5c0b\u6c42\u5354\u52a9\uff0c\u62... href=\"https://www.dropbox.com/home\"\u003e\u9996\u9801\u003c/a\u003e\u3002", "pt_BR": "\u003ch1\u003eErro (4xx) \u003c/h1\u003eN\u00e3o foi poss\u00edvel encontrar a p\u00e1gina que est\u00e1 procurando. Acesse a nossa \u003ca href=\"https://www.dropbox.com/help\"\u003eCentral de ajuda\u003c/a\u003e e \u003ca href=\"https://forums.dropbox.com\"\u003enossos f\u00f3runs\u003c/a\u003e para obter ajuda ou volte para  \u003ca href=\"https://www.dropbox.com/home\"\u003ea p\u00e1gina inicial\u003c/a\u003e.", "zh_CN": "\u003ch1\u003e\u9519\u8bef (4xx)\u003c/h1\u003e\u627e\u4e0d\u5230\u60a8\u641c\u7d22\u7684\u9875\u9762\u3002\u8bf7\u67e5\u9605\u6211\u4eec\u7684\u003ca href=\"https://www.dropbox.com/help\"\u003e\u5e2e\u52a9\u4e2d\u5fc3\u003c/a\u003e\u548c\u003ca href=\"https://forums.dropbox.com\"\u003e\u8bba\u575b\u003c/a\u003e\u83b7\u53d6\u5e2e\u52a9\uff0c\u6216\u8f... href=\"https://www.dropbox.com/home\"\u003e\u4e3b\u9875\u003c/a\u003e\u3002", "da_DK": "\u003ch1\u003eFejl (4xx)\u003c/h1\u003e Vi kan ikke finde den side, du leder efter. Tjek vores \u003ca href=\"https://www.dropbox.com/help\"\u003eHj\u00e6lpecenter\u003c/a\u003e og \u003ca href=\"https://forums.dropbox.com\"\u003eforummer\u003c/a\u003e for hj\u00e6lp, eller g\u00e5 tilbage til \u003ca href=\"https://www.dropbox.com/home\"\u003estartsiden\u003c/a\u003e.", "de": "\u003ch1\u003eFehler (4xx)\u003c/h1\u003eWir finden die Seite nicht, nach der Sie suchen. Schauen Sie sich im \u003ca href=\"https://www.dropbox.com/help\"\u003eHilfecenter\u003c/a\u003e und in den \u003ca href=\"https://forums.dropbox.com\"\u003eForen\u003c/a\u003e um, wenn Sie Hilfe ben\u00f6tigen, oder kehren Sie zur\u00fcck auf die \u003ca href=\"https://www.dropbox.com/home\"\u003eStartseite\u003c/a\u003e.", "ko": "\u003ch1\u003e\uc624\ub958(4xx)\u003c/h1\u003e\ucc3e\uc73c\ub824\ub294 \ud398\uc774\uc9c0\uac00 \uc5c6\uc2b5\ub2c8\ub2e4. \ub3c4\uc6c0\uc774 \ud544\uc694\ud55c \uacbd\uc6b0 Dropbox\uc758 \u003ca href=\"https://www.dropbox.com/help\"\u003e\ub3c4\uc6c0\ub9d0 \uc13c\ud130\u003c/a\u003e \ubc0f \u003ca href=\"https://forums.dropbox.com\"\u003e\ud3ec\ub7fc\u003c/a\u003e\uc744 \ucc38\uc870\ud558\uac70\ub098 \u003ca href=\"https://www.dropbox.com/home\"\u003e\ud648\u003c/a\u003e\uc73c\ub85c \ub3cc\uc544\uac00\uc138\uc694.", "it": "\u003ch1\u003eErrore (4xx)\u003c/h1\u003eNon riusciamo a trovare la pagina che stai cercando. Consulta il \u003ca href=\"https://www.dropbox.com/help\"\u003eCentro assistenza\u003c/a\u003e e i \u003ca href=\"https://forums.dropbox.com\"\u003eforum\u003c/a\u003e per assistenza oppure torna alla \u003ca href=\"https://www.dropbox.com/home\"\u003ehomepage\u003c/a\u003e.", "sv_SE": "\u003ch1\u003eFel (4xx)\u003c/h1\u003eVi kan inte hitta den sida du letar efter. Kolla in v\u00e5rt \u003ca href=\"https://www.dropbox.com/help\"\u003eHj\u00e4lpcenter\u003c/a\u003e och \u003ca href=\"https://forums.dropbox.com\"\u003ev\u00e5ra forum\u003c/a\u003e f\u00f6r att f\u00e5 hj\u00e4lp eller g\u00e5 tillbaka till \u003ca href=\"https://www.dropbox.com/home\"\u003estartsidan.\u003c/a\u003e", "uk_UA": "\u003ch1\u003e\u041f\u043e\u043c\u0438\u043b\u043a\u0430 (4xx)\u003c/h1\u003e\u041c\u0438 \u043d\u0435 \u043c\u043e\u0436\u0435\u043c\u043e \u0437\u043d\u0430\u0439\u0442\u0438 \u0441\u0442\u043e\u0440\u0456\u043d\u043a\u0443, \u044f\u043a\u0443 \u0432\u0438 \u0448\u0443\u043a\u0430\u0454\u0442\u0435. \u041f\u0435\u0440\u0435\u0433\u043b\u044f\u043d\u044c\u0442\u0435 \u003ca href=\"https://www.dropbox.com/help\"\u003e\u0426\u0435\u043d\u0442\u0440 \u0434\u043e\u0432\u0456\u0434\u043a\u0438\u003c/a\u003e \u0442\u0430 \u003ca href=\"https://forums.dropbox.com\"\u003e\u0444\u043e\u0440\u0443\u043c\u0438\u003c/a\u003e \u0430\u0431\u043e \u043f\u043e\u0432\u0435\u0440\u043d\u0456\u0442\u044c\u0441\u044f \u043d\u0430 \u003ca href=\"https://www.dropbox.com/home\"\u003e\u0433\u043e\u043b\u043e\u0432\u043d\u0443 \u0441\u0442\u043e\u0440\u0456\u043d\u043a\u0443\u003c/a\u003e.", "nl_NL": "\u003ch1\u003eFout (4xx)\u003c/h1\u003eHet is niet gelukt om de pagina te vinden waarnaar je op zoek bent. Kijk in ons \u003ca href=\"https://www.dropbox.com/help\"\u003ehelpcentrum\u003c/a\u003e en de \u003ca href=\"https://forums.dropbox.com\"\u003eforums\u003c/a\u003e voor hulp of ga terug naar de \u003ca href=\"https://www.dropbox.com/home\"\u003ebeginpagina\u003c/a\u003e.", "es": "\u003ch1\u003eError (4xx)\u003c/h1\u003eNo podemos encontrar la p\u00e1gina que buscas. Visita nuestro \u003ca href=\"https://www.dropbox.com/help\"\u003ecentro de ayuda\u003c/a\u003e y \u003ca href=\"https://forums.dropbox.com\"\u003eforos\u003c/a\u003e para obtener asistencia, o dir\u00edgete nuevamente al \u003ca href=\"https://www.dropbox.com/home\"\u003einicio\u003c/a\u003e.", "ms": "\u003ch1\u003eRalat (4xx)\u003c/h1\u003eKami tidak dapat menemui halaman yang anda sedang cari. Lihat \u003ca href=\"https://www.dropbox.com/help\"\u003ePusat Bantuan\u003c/a\u003e dan \u003ca href=\"https://forums.dropbox.com\"\u003eforum\u003c/a\u003e kami untuk bantuan, atau kembali ke \u003ca href=\"https://www.dropbox.com/home\"\u003erumah\u003c/a\u003e.", "id": "\u003ch1\u003eKesalahan (4xx)\u003c/h1\u003eKami tidak bisa menemukan halaman yang Anda cari. Kunjungi \u003ca href=\"https://www.dropbox.com/help\"\u003ePusat Bantuan\u003c/a\u003e dan \u003ca href=\"https://forums.dropbox.com\"\u003eforum\u003c/a\u003e kami untuk mendapatkan bantuan, atau kembalilah ke \u003ca href=\"https://www.dropbox.com/home\"\u003ehalaman awal\u003c/a\u003e.", "th_TH": "\u003ch1\u003e\u0e02\u0e49\u0e2d\u0e1c\u0e34\u0e14\u0e1e\u0e25\u0e32\u0e14 (4xx)\u003c/h1\u003e\u0e40\u0e23\u0e32\u0e44\u0e21\u0e48\u0e1e\u0e1a\u0e2b\u0e19\u0e49\u0e32\u0e17\u0e35\u0e48\u0e04\u0e38\u0e13\u0e01\u0e33\u0e25\u0e31\u0e07\u0e21\u0e2d\u0e07\u0e2b\u0e32 \u0e42\u0e1b\u0e23\u0e14\u0e14\u0e39\u0e23\u0e32\u0e22\u0e25\u0e30\u0e40\u0e2d\u0e35\u0e22\u0e14\u0e17\u0e35\u0e48 \u003ca href=\"https://www.dropbox.com/help\"\u003e\u0e28\u0e39\u0e19\u0e22\u0e4c\u0e04\u0e27\u0e32\u0e21\u0e0a\u0e... \u0e41\u0e25\u0e30 \u003ca href=\"https://forums.dropbox.com\"\u003e\u0e1f\u0e2d\u0e23\u0e31\u0e21\u003c/a\u003e \u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e02\u0e2d\u0e04\u0e27\u0e32\u0e21\u0e0a\u0e48\u0e27\u0e22\u0e40\u0e2b\u0e25\u0e37\u0e2d \u0e2b\u0e23\u0e37\u0e2d\u0e01\u0e25\u0e31\u0e1a\u0e44\u0e1b\u0e17\u0e35\u0e48 \u003ca href=\"https://www.dropbox.com/home\"\u003e\u0e2b\u0e19\u0e49\u0e32\u0e41\u0e23\u0e01\u003c/a\u003e", "es_ES": "\u003ch1\u003eError (4xx)\u200b \u003c/h1\u003eNo encontramos la p\u00e1gina que est\u00e1s buscando. Consulta nuestro \u003ca href=\"https://www.dropbox.com/help\"\u003eCentro de ayuda\u003c/a\u003e  y los \u003ca href=\"https://forums.dropbox.com\"\u003eforos\u003c/a\u003e  para obtener asistencia o vuelve a la \u003ca href=\"https://www.dropbox.com/home\"\u003ep\u00e1gina principal\u003c/a\u003e.", "ja": "\u003ch1\u003e\u30a8\u30e9\u30fc (4xx)\u003c/h1\u003e\u304a\u63a2\u3057\u306e\u30da\u30fc\u30b8\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002\u30d8\u30eb\u30d7\u304c\u5fc5\u8981\u306a\u5834\u5408\u306f\u3001\u003ca href=\"https://www.dropbox.com/help\"\u003e\u30d8\u30eb\u30d7\u30bb\u30f3\u30bf\u30fc\u003c/a\u003e\u3084\u... href=\"https://forums.dropbox.com\"\u003e\u30d5\u30a9\u30fc\u30e9\u30e0\u003c/a\u003e\u3092\u3054\u89a7\u30... href=\"https://www.dropbox.com/home\"\u003e\u30db\u30fc\u30e0\u003c/a\u003e\u306b\u623b\u3063\u3066\u304f\u...", "pl": "\u003ch1\u003eB\u0142\u0105d (4xx)\u003c/h1\u003eNie mo\u017cna znale\u017a\u0107 \u017c\u0105danej strony. Odwied\u017a \u003ca href=\"https://www.dropbox.com/help\"\u003eCentrum pomocy\u003c/a\u003e i \u003ca href=\"https://forums.dropbox.com\"\u003efora\u003c/a\u003e, aby uzyska\u0107 pomoc, lub przejd\u017a do \u003ca href=\"https://www.dropbox.com/home\"\u003estrony g\u0142\u00f3wnej\u003c/a\u003e."};

function read_cookie (name) {

        var nameEQ = name + "=";

        var ca = document.cookie.split(';');

        for (var i = 0; i < ca.length; i++) {

            var c = ca[i];

            while (c.charAt(0) == ' ') {

                c = c.substring(1, c.length);

            }

            if (c.indexOf(nameEQ) === 0) {

                return c.substring(nameEQ.length, c.length);

            }

        }

        return null;

}

Event.observe(document, 'dom:loaded', function () {

    var locale = read_cookie('locale');

    if (locale) {

       var msg = message[locale];

       if (msg) {

           $('errorbox').update(msg);

       }

    }

});

</script>

 

 

 

Re: How do I upload a file to my dropbox apps folder in dropbox in objective c?

Dropboxer
Dropboxer

The forum formatting may have messed this up, but make sure you don't have any "\"s. 

Also, make sure you properly replaced all of the *_HERE values from my sample.

Re: How do I upload a file to my dropbox apps folder in dropbox in objective c?

Chez B. New member | Level 1
New member | Level 1

What does NONCE refer to and do I just supply a bash function for the timestamp portion?

In addition, is there a reason why I am not receiving the callbacks log messages? I have not seen one yet.

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?