We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
Crypto C.
11 years agoNew member | Level 1
OAuth 2.0 - IOS API V 2.0
Hello !
I use the the version API dropbox 1.0 for my app IOS, the new 2.0 is only swift and i can not use it because my app use only object-c.
So now i use API 1.0 for generate autentication DBSession restClient etc then use "https://api.dropboxapi.com/1/oauth2/token_from_oauth1" for access_token and call via HTTP the new api.dropboxapi.com/2.
This method works correctly, my question is, if this method is a good way to take advantage of the new API V2 ?
thanks
m.
16 Replies
Replies have been turned off for this discussion
- Greg-DB11 years ago
Dropbox Community Moderator
Yes, you can certainly use that endpoint to get an OAuth 2 access token and then use the HTTP interface for API v2.
- Crypto C.11 years agoNew member | Level 1
ok, thanks Gregory, another question if possible, i have problem with parameters for API v2, for example :
NSString *urlWithParams = @"https://api.dropboxapi.com/2/sharing/get_shared_links";
NSURL *url = [NSURL URLWithString:urlWithParams];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:[appDelegate apiAuthorizationHeader] forHTTPHeaderField:@"Authorization"];
NSDictionary *parameters = @{@"path": @"};
NSMutableString *parameterString = [NSMutableString string];
for (NSString *key in [parameters allKeys]) {
if ([parameterString length]) {
[parameterString appendString:@"&"];
}
[parameterString appendFormat:@"%@=%@", key, parameters[key]];
}
if (parameterString) [request setHTTPBody:[parameterString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data,
...
the problem is :
NSDictionary *parameters = @{@"path": @"};
NSMutableString *parameterString = [NSMutableString string];
for (NSString *key in [parameters allKeys]) {
if ([parameterString length]) {
[parameterString appendString:@"&"];
}
[parameterString appendFormat:@"%@=%@", key, parameters[key]];
}
if (parameterString) [request setHTTPBody:[parameterString dataUsingEncoding:NSUTF8StringEncoding]];
the format is incorrect ... where is the problem ??
thanks
m.
- Greg-DB11 years ago
Dropbox Community Moderator
It looks like you're form encoding the parameter(s) that you send in the body, but the API actually just expects JSON:
https://www.dropbox.com/developers/documentation/http#documentation-sharing-get_shared_links
For example, in this case, the data you send up should just be:
"{"path": "}"
- Crypto C.11 years agoNew member | Level 1
yes, mmm please, if possible, write small example in object-c ...
i have tested with :
...
NSString *urlWithParams = @"https://api.dropboxapi.com/2/sharing/get_shared_links";
NSURL *url = [NSURL URLWithString:urlWithParams];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:[appDelegate apiAuthorizationHeader:2] forHTTPHeaderField:@"Authorization"];
NSDictionary *parameters = @{@"path" : @"};
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error];
[request setHTTPBody:postdata];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
but too error 400 ...
- Greg-DB11 years ago
Dropbox Community Moderator
What's the body of the error response? It should contain some more useful information.
- Crypto C.11 years agoNew member | Level 1
no this is httpResponse :
<NSHTTPURLResponse: 0x7fa7438723f0> { URL: https://api.dropboxapi.com/2/sharing/get_shared_links } { status code: 400, headers {
Connection = "keep-alive";
"Content-Type" = "text/plain; charset=utf-8";
Date = "Tue, 24 Nov 2015 19:15:03 GMT";
Server = nginx;
"Transfer-Encoding" = Identity;
"X-Dropbox-Request-Id" = 998c7ed7b7368bf59b7753ce5ae43308;
} }
- Greg-DB11 years ago
Dropbox Community Moderator
That only seems to be showing the status code and headers. What is the body of the response?
- Crypto C.11 years agoNew member | Level 1
Error in call to API function "sharing/get_shared_links": Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded". Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack".
- Greg-DB11 years ago
Dropbox Community Moderator
That seems to be it then. The API expects a "Content-Type" header of "application/json", but your code apparently sends up "application/x-www-form-urlencoded". Change that to "application/json" and try again.
- Crypto C.11 years agoNew member | Level 1
ok,
add :
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
and now it's ok ....
good work.
About Dropbox API Support & Feedback
Find help with the Dropbox API from 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!