Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

513 error when running api v 2 on two different devices #60

Closed
markmcminn opened this issue Nov 12, 2016 · 35 comments
Closed

513 error when running api v 2 on two different devices #60

markmcminn opened this issue Nov 12, 2016 · 35 comments

Comments

@markmcminn
Copy link

My app uses Dropbox to download files and store them temporarily on the device until they are uploaded to iCloud. I've followed the client.fileRoutes downloadURL method on api v2 exactly as described in the tutorial and it works great on either my iPhone or iPad. But once it's running on one device, I get a 513 error on the other:

NSError = "Error Domain=NSCocoaErrorDomain Code=513 CFNetworkDownload_ok6tFU.tmp\U201d couldn't be moved because you don't have permission to access..."

This is true whether I run the app directly from Xcode or I download it from the App Store and run it that way. Both devices are running from the same Dropbox account.

I've tried all sorts of things to work around this, but can't find any. It doesn't seem to be a write permissions issue on the device (I've tested for this), but rather a permission to access issue on Dropbox.

Thanks for any insight you can offer me!

img_1696

@scobbe
Copy link
Contributor

scobbe commented Nov 14, 2016

@markmcminn: Thanks for reaching out. Looks like the Dropbox SDK is having trouble moving a recently downloaded file from it's temporary location to it's final destination.

Usually the download flow for the SDK looks like:

  • Use NSURLSession library to download a file from Dropbox to a temporary (library-defined) location
  • Move the file to an SDK-defined temporary location (so it isn't garbage collected)
  • Then, when the user supplied handler code is finally ready to be executed, move the file from its SDK-defined temp location to its user-defined permanent location, then execute the response code

It would be helpful to get a stack trace of the failure so I can identify which move operation in the above flow is failing.

Also, I would ask you to examine closely the value you're supply for the destination parameter in your call to downloadUrl:overwrite:destination:.

Based on this error, it is possible that your app logic is generating a destination url for a destination that is only accessible from one of the apps and not the other. Not totally sure though.

Would you mind telling me a little bit more about how your app uses the Dropbox API? Is it designed to automatically download everything from a user's Dropbox account and upload it to iCloud? Are there specific conditions that trigger a download from Dropbox?

@markmcminn
Copy link
Author

markmcminn commented Nov 15, 2016

Thanks for the reply. This is a competency based app that is used to give feedback to doctoral students on course assignments. If the assignment is a Word document, for example, the faculty member marks it up on his or her computer and then saves it to a Dropbox file. Then in the iOS app the faculty member completes a rubric, makes comments, and then attaches the file from Dropbox. The email is sent to the student with the Dropbox file as attachment. When the rubric score is uploaded to iCloud, the Word file from Dropbox is also uploaded and stored as a CKAsset in iCloud. It all works perfectly on my iPad right now, but I get the error described above on my iPhone. Last week, with the same code, it was just the opposite. It was working great on my iPhone, but I got this error on my iPad.

Regarding the destination url, here is the code I am using, which is modeled after the sample I found on your github site (very helpful, by the way!):

DropboxClient *client = [DropboxClientsManager authorizedClient];
NSDictionary *outerDictionary = [self.fileDictionariesToDisplay objectAtIndex:indexPath.row];
__block NSDictionary *newOuterDictionary;

NSString *outputFileName = [NSString stringWithFormat:@"Attachment%lu",(unsigned long)self.fileDictionariesToAttach.count+1];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *outputDirectory = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
NSURL *outputUrl = [outputDirectory URLByAppendingPathComponent:outputFileName];

[[[client.filesRoutes downloadUrl:filePath overwrite:YES destination:outputUrl]
  response:^(DBFILESFileMetadata *result, DBFILESDownloadError *routeError, DBError *error, NSURL *destination)
{
    if (result)
    {
        //NSData *fileData = [[NSFileManager defaultManager] contentsAtPath:[destination path]];

        //NSLog(@"File Contents: %@\n", fileData);
        //NSLog(@"File data length: %lu",(unsigned long)fileData.length);
        //NSLog(@"File path: %@",filePath);

        //reset array and reload table
        newOuterDictionary = @{@"Metadata":[outerDictionary valueForKey:@"Metadata"], @"isDownloaded":@YES, @"isDownloadingNow":@NO};

        //update table
        [self.fileDictionariesToDisplay replaceObjectAtIndex:indexPath.row withObject:newOuterDictionary];
        [self.artifactFileTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

        //update fileDictionariesToAttach
        NSDictionary *fileDictionary = @{@"attachmentPath":outputUrl, @"filePath":filePath, @"shouldBeAttached":@YES};
        [self.fileDictionariesToAttach addObject:fileDictionary];

        self.currentlyDownloadingAnotherFile = NO;
    }
    else
    {
        //NSLog(@"Error %@",error.description);

        NSLog(@"Stack trace : %@",[NSThread callStackSymbols]);

        NSString *title = @"";
        NSString *message = @"";
        if (routeError)
        {
            // Route-specific request error
            title = @"Route-specific error";
            if ([routeError isPath])
            {
                message = [NSString stringWithFormat:@"Invalid path: %@", routeError];
            }
            else if ([routeError isOther])
            {
                message = [NSString stringWithFormat:@"Unknown error: %@", routeError];
            }
        }
        else
        {
            // Generic request error
            title = @"Generic request error";
            if ([error isInternalServerError])
            {
                DBRequestInternalServerError *internalServerError = [error asInternalServerError];
                message = [NSString stringWithFormat:@"%@", internalServerError];
            }
            else if ([error isBadInputError])
            {
                DBRequestBadInputError *badInputError = [error asBadInputError];
                message = [NSString stringWithFormat:@"%@", badInputError];
            }
            else if ([error isAuthError])
            {
                DBRequestAuthError *authError = [error asAuthError];
                message = [NSString stringWithFormat:@"%@", authError];
            }
            else if ([error isRateLimitError])
            {
                DBRequestRateLimitError *rateLimitError = [error asRateLimitError];
                message = [NSString stringWithFormat:@"%@", rateLimitError];
            }
            else if ([error isHttpError])
            {
                DBRequestHttpError *genericHttpError = [error asHttpError];
                message = [NSString stringWithFormat:@"%@", genericHttpError];
            }
            else if ([error isClientError])
            {
                DBRequestClientError *genericLocalError = [error asClientError];
                message = [NSString stringWithFormat:@"%@", genericLocalError];
            }
        }

        UIAlertController *alertController =
        [UIAlertController alertControllerWithTitle:title
                                            message:message
                                     preferredStyle:(UIAlertControllerStyle)UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"Okay"
                                                            style:(UIAlertActionStyle)UIAlertActionStyleCancel
                                                          handler:nil]];
        [self presentViewController:alertController animated:YES completion:nil];
    }

It works fine on both the iPad and iPhone when it's working, but when it is not working I get the error.

I've not done a Stack Trace before. Does this look like what you need?

(
0 CLAS Faculty 0x00000001000f6068 __48-[ArtifactSelectorVC downloadFile:AndIndexPath:]_block_invoke + 1156
1 ObjectiveDropboxOfficial 0x00000001004a72f8 __39-[DBDownloadUrlTask response:response:]_block_invoke + 932
2 ObjectiveDropboxOfficial 0x00000001004cb8c0 -[DBDelegate URLSession:downloadTask:didFinishDownloadingToURL:] + 772
3 CFNetwork 0x000000019441fe78 + 36
4 Foundation 0x000000019472e754 + 16
5 Foundation 0x00000001946732c8 + 96
6 Foundation 0x00000001946638c4 + 620
7 Foundation 0x0000000194730b00 + 228
8 libdispatch.dylib 0x000000010104521c _dispatch_client_callout + 16
9 libdispatch.dylib 0x000000010104a284 _dispatch_main_queue_callback_4CF + 1200
10 CoreFoundation 0x0000000193c17f2c + 12
11 CoreFoundation 0x0000000193c15b18 + 1660
12 CoreFoundation 0x0000000193b44048 CFRunLoopRunSpecific + 444
13 GraphicsServices 0x00000001955ca198 GSEventRunModal + 180
14 UIKit 0x0000000199b302fc + 684
15 UIKit 0x0000000199b2b034 UIApplicationMain + 208
16 CLAS Faculty 0x00000001001196e4 main + 124
17 libdyld.dylib 0x0000000192b285b8 + 4
)

Thanks for looking into this!

Mark

@markmcminn
Copy link
Author

Now I'm getting the error on both of my devices. The code hasn't changed.

@markmcminn
Copy link
Author

Here is the destination url where the file is to be written on the iPad. file:///var/mobile/Containers/Data/Application/D1C08C61-3A05-4113-ADFE-A2A9D103670E/Documents/Attachment1

Here is the error message:
NSError = "Error Domain=NSCocoaErrorDomain Code=513 "\U201cCFNetworkDownload_MwlHQw.tmp\U201d couldn’t be moved because you don’t have permission to access \U201cDocuments\U201d."

UserInfo={NSSourceFilePathErrorKey=/var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/com.114consulting.CLASFaculty/CFNetworkDownload_MwlHQw.tmp, NSUserStringVariant=(\n Move\n), NSDestinationFilePath=/var/mobile/Containers/Data/Application/D1C08C61-3A05-4113-ADFE-A2A9D103670E/Documents/Attachment1, NSFilePath=/var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/com.114consulting.CLASFaculty/CFNetworkDownload_MwlHQw.tmp, NSUnderlyingError=0x1706459a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}";

It looks to me like it's not allowing the file to be moved from the temporary location you mentioned in your earlier email.

@markmcminn
Copy link
Author

Okay, here's a bit more information. I traced the error to the Dropbox Framework. It's occurring in this method:

  • (void)URLSession:(NSURLSession *)session
    downloadTask:(NSURLSessionDownloadTask *)downloadTask
    didFinishDownloadingToURL:(NSURL *)location

There is a an if (handlerQueue) a few lines in, and then an else {responseHandler(location, downloadTask.response, nil);} clause. The error is occurring in response to the else clause in this if-then-else statement. Does this help?

@markmcminn
Copy link
Author

And a bit more information... If I use the client.filesRoutes downloadData method instead of client.filesRoutes downloadUrl, I don't get the 513 error. Everything seems to work fine in terms of progress monitoring and the metadata download, but the NSData object is nil at the end of the download.

@markmcminn
Copy link
Author

Got it working today by resetting my devices to factory settings. Perhaps things will work for a while now, but I still think there are some issues here.

@scobbe
Copy link
Contributor

scobbe commented Dec 1, 2016

@markmcminn: Sorry we couldn't be more helpful. Let us know if the issue persists, in which case we can really dive in to get to the bottom of it.

@douglashill
Copy link

douglashill commented Jan 12, 2017

I saw a similar problem (not using Dropbox SDK) where the location passed to URLSession:downloadTask:didFinishDownloadingToURL: was /var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/<bundle_id>/CFNetworkDownload_<id>.tmp, which the app can’t read because it’s outside the sandbox. It only happened on a particular device for a particular app, and went away after a reboot.

I suspect this is a bug in NSURLSession, where it sometimes doesn’t move the downloaded file into the sandbox container.

@claurel
Copy link

claurel commented Mar 7, 2017

I found this thread while searching for a solution to the same problem. I was able to work around it in my app by setting the sharedContainerIdentifier of the URLSession. The app's extensions don't actually need to use any of the downloaded content, but setting the value to the app group identifier seems to have the effect of keeping the downloaded files in the sandbox container.

@markmcminn
Copy link
Author

markmcminn commented Mar 7, 2017 via email

@scobbe
Copy link
Contributor

scobbe commented Mar 7, 2017

@claurel: Thanks for the suggestion. @markmcminn: Let us know if that solves the issue for you. I can push out a change to expose sharedContainerIdentifier and set it to the app group identifier by default.

@markmcminn
Copy link
Author

markmcminn commented Mar 8, 2017 via email

@scobbe
Copy link
Contributor

scobbe commented Mar 8, 2017

@markmcminn: That's great to hear. Just wanted to clarify – did you see this error occurring in the context of using an app extension, or no?

@markmcminn
Copy link
Author

markmcminn commented Mar 8, 2017 via email

@scobbe
Copy link
Contributor

scobbe commented Mar 8, 2017

@markmcminn: Got it, thanks!

@scobbe
Copy link
Contributor

scobbe commented Mar 10, 2017

@markmcminn: sharedContainerIdentifier is now exposed in the latest version of the SDK.

Thanks for your patience, and let us know if anything else comes up.

@scobbe scobbe closed this as completed Mar 10, 2017
@guidedways
Copy link

I should add I'm seeing this almost every single time and I'm not using an App Extension. I'll try this solution out.

@scobbe
Copy link
Contributor

scobbe commented Apr 3, 2017

@guidedways: Any luck?

@guidedways
Copy link

@scobbe sorry was still testing - I think that fixed it for me as I've stopped seeing these errors spring up. Weird!

@scobbe
Copy link
Contributor

scobbe commented Apr 4, 2017

@guidedways: Good to hear the issue was resolved. Were you using multiple devices / build targets at the time of the issue?

@markmcminn
Copy link
Author

In my case, yes, multiple devices (iPhone + iPad). The latest version of the SDK has cleared it up for me. Thanks!

@alebaldi
Copy link

alebaldi commented May 4, 2017

@markmcminn can you help me? I have same problem that you describe before: "And a bit more information... If I use the client.filesRoutes downloadData method instead of client.filesRoutes downloadUrl, I don't get the 513 error. Everything seems to work fine in terms of progress monitoring and the metadata download, but the NSData object is nil at the end of the download."

I just use last DropBox SDS, downloaded in April 11, 2017.

Where is the problem?

Sometimes download go well, but 90% of times give me error that you describe...

Thanks

@markmcminn
Copy link
Author

@alebaldi I'm sorry to hear about the problem. The latest SDK cleared it up for me so I'm not sure what might be happening in your situation. Before the latest SDK, I had to completely reset my device (iPad or iPhone) and then when I reinstalled the app it worked okay for a while. If I used the device both for development and also to run my app from the App Store, then the problem started again. You might try a full reset on your device and see if that helps.

@alebaldi
Copy link

alebaldi commented May 4, 2017

@markmcminn what is versione of SDK that resolve your problem? Whit this version you must set trasnportConfig or not?
I can't reset my device so easy... it's my personale phone... :-(
Thanks

@markmcminn
Copy link
Author

It was version 3.0.4 released on March 9 that worked for me. Yes, mine was my personal phone also. It was a pain. No, the DBTransportClient was no longer necessary with the March 9 release.

@alebaldi
Copy link

alebaldi commented May 4, 2017

I downloaded SDK in first day of April... so my version I think is correct... :-(... but I have this problem... I don't know what to do...

@markmcminn
Copy link
Author

Yes, it is probably the current version. And you're not using the DBTransportClient code, right? I had to take that code out with the latest version. If all this is correct, I have no idea what is going on. You may need to file a bug report.

@alebaldi
Copy link

alebaldi commented May 4, 2017

I have same problem with or without DBTransportClient... nothing it's changed... Only one thing to note: first day of my test it's all ok... for all day... but starting from second day I had this problem... What's changed?! In my code... nothings...

@scobbe
Copy link
Contributor

scobbe commented May 4, 2017

@alebaldi: Could you update to the latest version of the SDK, and the let me know if your problem is still there?

@scobbe
Copy link
Contributor

scobbe commented May 4, 2017

@alebaldi: I think I might have determined with the issue is. Please try updating to the latest version of the SDK, v3.0.18, and see if that fixes your issue.

@alebaldi
Copy link

alebaldi commented May 5, 2017

@scobbe with last version, 3.0.18, it's all ok. I have three App, and all Apps now it's all ok. Thank you very much! ;-)

@scobbe
Copy link
Contributor

scobbe commented May 5, 2017

@alebaldi: That's great to hear! Let us know if anything else comes up, or if you have feedback for the SDK.

Good luck!

@guidedways
Copy link

@scobbe We were getting a lot of reports of hang-ups and other strange timeouts which suddenly seem to have disappeared for those users (currently in beta). I was using v3.0.9 and just updated to 3.0.18 - looks like there are a number of improvements packed in. Thanks!

@scobbe
Copy link
Contributor

scobbe commented May 5, 2017

@guidedways: That's great to hear! Hopefully, things should be more stable going forward. Let us know if you have any more suggestions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants