iOS:调用具有良好授权的https://api.dropboxapi.com/2/users/get_current_account时,代码状态为400

时间:2019-07-09 18:09:48

标签: ios dropbox nsurlsession nsurlrequest nsurlsessiondatatask

我试图调用保管箱网址https://api.dropboxapi.com/2/users/get_current_account来检查令牌访问是否良好。

它在Postman上像超级魅力,但在iOS上不起作用。

这是邮递员电话:
在标题中:
  Headers 在体内:
Body

此代码:

            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);
                }
            }];

(不起作用,无法识别功能响应:^。

这是我的代码:

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];

(也不起作用,代码状态为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
    );
} }

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您应该尝试以下操作:

[[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);
                    }
                }];