11

I tried

download.file('https://www.dropbox.com/s/r3asyvybozbizrm/Himalayas.jpg',
              destfile="1.jpg",
              method="auto")

but it returns the HTML source of that page.

Also tried a little bit of rdrop

library(rdrop2)
# please put in your key/secret
drop_auth(new_usesr = FALSE, key=key, secret=secret, cache=T)

And the pop up website reports:

Invalid redirect_uri: "http://localhost:1410": It must exactly match one of the redirect URIs you've pre-configured for your app (including the path).

I don't understand the URI thing very well. Can somebody recommend some document to read please....

I read some posts but most of them discuss how to read data from excel files.

repmis worked only for reading excel files...

library(repmis)
repmis::source_DropboxData("test.csv",
                           "tcppj30pkluf5ko",
                           sep = ",",
                           header = F)

Also tried

library(RCurl)
url='https://www.dropbox.com/s/tcppj30pkluf5ko/test.csv'
x = getURL(url)
read.csv(textConnection(x))

And it didn't work...

Any help and discussion's appreciated. Thanks!

YJZ
  • 3,934
  • 11
  • 43
  • 67
  • hi Thanks @Pascal! Actually I tired a little bit of that - don't understand the URI... see update in OP please – YJZ Aug 12 '15 at 04:31

2 Answers2

15

The first issue is because the https://www.dropbox.com/s/r3asyvybozbizrm/Himalayas.jpg link points to a preview page, not the file content itself, which is why you get the HTML. You can modify links like this though to point to the file content, as shown here:

https://www.dropbox.com/help/201

E.g., add a raw=1 URL parameter:

https://www.dropbox.com/s/r3asyvybozbizrm/Himalayas.jpg?raw=1

Your downloader will need to follow redirects for that to work though.

The second issue is because you're trying to use a OAuth 2 app authorization flow, which requires that all redirect URIs be pre-registered. You can register redirect URIs, in your case it's http://localhost:1410, for Dropbox API apps on the app's page on the App Console:

https://www.dropbox.com/developers/apps

For more information on using OAuth, you can refer to the Dropbox API OAuth guide here:

https://www.dropbox.com/developers/reference/oauthguide

Greg
  • 16,359
  • 2
  • 34
  • 44
  • Thanks a lot @Greg! I used "?raw=1" and the download was successful. R displays `Content type 'image/jpeg' length 67704 bytes (66 KB) downloaded 66 KB`. `67704` is the correct size of that picture. But the property of the downloaded picture says `67,910 bytes`, and I can't open it... Any idea why it's like this? Thank you! – YJZ Aug 12 '15 at 05:44
  • When I try download the raw version of the link via my browser, it yields a 67,704 byte file, which I can open successfully. Is your downloading code altering it for some reason? Have you tried diffing the two versions to see what the difference is? – Greg Aug 12 '15 at 18:00
  • Hi @Greg thanks for your help. I can download it through browser too, but didn't get it work in Rstudio. What two versions do you mean? I used `download.file('https://www.dropbox.com/s/r3asyvybozbizrm/Himalayas.jpg?raw=1', destfile="1.jpg", method="auto")`. Rstudio shows `Content type 'image/jpeg' length 67704 bytes (66 KB)`, but right-click on file -> property shows `67,910 bytes`, and I can't open the pic. Let me know please. Thanks~ – YJZ Aug 12 '15 at 23:23
  • I meant the valid 67,704 byte version, e.g., from your browser, and the invalid 67,910 byte version, which you are getting via Rstudio. – Greg Aug 12 '15 at 23:37
  • I see. Thank you @Greg! I used `library(jpeg)` `pic.bad = readJPEG("1.jpg")` `pic.good = readJPEG("Himalayas.jpg")`. The good picture was loaded as a large array, the bad picture was not and R shows: `JPEG decompression: Corrupt JPEG data: 10 extraneous bytes before marker 0xfeError in readJPEG("1.jpg") : JPEG decompression error: Bogus DQT index 12`. Let me know if this info helps. Did you give it a try to download the pic from dropbox in R? Thank you so much – YJZ Aug 12 '15 at 23:49
  • I haven't tried it in R, nor do I have much experience with R, and since the Dropbox link itself is working properly, I'm afraid I won't be able to offer much more insight. I do suggest pulling that bad copy out of R though and manually inspecting and comparing it to see what is different about it. – Greg Aug 12 '15 at 23:53
  • Thanks @Greg! Do you know how to manually inspect that? I opened both in notepad++, and they should weird characters. I converted them to base 64 encode and now there are all letters and numbers and /'s. I notice the beginning of both files are the same, and the end are different. Should I manually find which part should I delete from the end of the bad file? Please let me know or recommend something to read about this. Thanks a lot – YJZ Aug 13 '15 at 00:00
0

I use read.table(url("yourdropboxpubliclink")) for instance I use this link

instead of using https://www.dropbox.com/s/xyo8sy9velpkg5y/foo.txt?dl=0, which is chared link on dropbox I use https://dl.dropboxusercontent.com/u/15634209/histogram/foo.txt

and non-public link raw=1 will work

It works fine for me.