iosdropboxnsurlsessionnsurlrequestnsurlsessiondatatask

iOS: Code Status 400 when calling https://api.dropboxapi.com/2/users/get_current_account with the good Authorization


I tried to call the dropbox url https://api.dropboxapi.com/2/users/get_current_account to check if the token access is good.

It works like a charm on Postman, but it's not working on iOS.

Here is the post man call:
In the Headers:
Headers In the body:
Body

This code:

            DBUserClient *client = [[DBUserClient alloc] initWithAccessToken:@"-sWPE_Fqxxxxxxxxxxxxxxxxxxxfdhgfdf82mk"];

            [[client.usersRoutes.getCurrentAccount] response:^(DBUSERSFullAccount *account, DBNilObject *_, DBRequestError *error) {
                if (account) {
                    NSLog(@"%@", account);
                } else if (error) {
                    NSLog(@"%@", error);
                }
            }];

(Doesn't work, the function response:^ is not recognized.

And this is my code:

DBUserClient *client = [[DBUserClient alloc] initWithAccessToken:@"-sWPE_FqvVAAxxxxxxxxxakjhdazhdzatEQGUd82mk"];


                NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: @"https://api.dropboxapi.com/2/users/get_current_account"]];

                [urlRequest setHTTPMethod:@"POST"];

                NSData *postData = [NSData new];
                [urlRequest setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postData length]] forHTTPHeaderField:@"Content-Length"];
                [urlRequest setHTTPBody:postData];


                NSURLSession *session = [NSURLSession sharedSession];
                NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                    NSLog(@"code status: %ld", (long)httpResponse.statusCode);
                    if (httpResponse.statusCode == 200)
                    {
                        NSError *parseError = nil;
                        NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
                        NSLog(@"The response is - %@",responseDictionary);
                        NSInteger success = [[responseDictionary objectForKey:@"success"] integerValue];
                        if(success == 1)
                        {
                            NSLog(@"Login SUCCESS");
                        }
                        else
                        {
                            NSLog(@"Login FAILURE");
                        }
                    }
                    else
                    {
                        NSLog(@"Error");
                    }
                }];
                [dataTask resume];

(Doesn't work too, there is a code status 400)

NSHTTPURLResponse:

<NSHTTPURLResponse: 0x281fd3c20> { URL: https://api.dropboxapi.com/2/users/get_current_account } { Status Code: 400, Headers {
    "Content-Disposition" =     (
        "attachment; filename='error'"
    );
    "Content-Type" =     (
        "text/plain; charset=utf-8"
    );
    Date =     (
        "Wed, 10 Jul 2019 08:18:40 GMT"
    );
    Server =     (
        nginx
    );
    "content-security-policy" =     (
        "sandbox; frame-ancestors 'none'"
    );
    "x-content-type-options" =     (
        nosniff
    );
    "x-dropbox-request-id" =     (
        17d523a27f864ce88531b122b4bec71d
    );
    "x-frame-options" =     (
        DENY
    );
} }

Any ideas?


Solution

  • You should try this:

    [[client.usersRoutes getCurrentAccount] setResponseBlock:^(DBUSERSFullAccount * _Nullable result, DBNilObject * _Nullable routeError, DBRequestError * _NullablenetworkError)
                    {
                        if (networkError != nil)
                        {
                            NSLog(@"getCurrentAccount failed: network error");
                        }
                        else if (routeError != nil)
                        {
                            NSLog(@"getCurrentAccount failed: route error");
                        }
                        else
                        {
                            NSLog(@"got Dropbox account email: %@", result.email);
                        }
                    }];