Red Mak
Red Mak

Reputation: 676

Get access to user data from box cloud (box.com) in ios

I'm trying to get data from box user account. For now i have generated the token but i can't get the root folder content (for example).

to get the token, i get the ticket with this request:

https://www.box.com/api/1.0/rest?action=get_ticket&api_key=BoxApiKey

After that i call a webView in my application to load this page :

https://m.box.com/api/1.0/auth/ticket

after the user log successfully i parse the web page and extract the token.

now come problems, i can't get the root folder content. this is what i try with AFNetworking:

  1. i add this methode in the AFHTTPClient class (as suggested her)

    -(void)setAuthorizationHeaderWithTokenBearer:(NSString *)token
    {
       [self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Bearer %@", token]];
    }
    
  2. my call:

    NSURL *url = [NSURL URLWithString:@"https://www.box.com/api/2.0/folders/0"];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
    [httpClient setAuthorizationHeaderWithTokenBearer:token];
    
    
    [httpClient postPath:@"" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"Request Successful, response '%@'", responseStr);
    } failure:^(AFHTTPRequestOperation *operation, NSError *e) {
        NSLog(@"[HTTPClient Error]: %@", e.localizedDescription);
    }];
    

But i have this localizedDescription :

[HTTPClient Error]: Expected status code in (200-299), got 401

can you help me to resolve this? thank you.

Upvotes: 1

Views: 357

Answers (1)

seanrose
seanrose

Reputation: 8685

You're seeing an issue because the header style you're using is OAuth 2, and the token you have is V1 Auth.

If you're using this header

Authorization: Bearer {ACCESS_TOKEN}

You need to get the token through the [OAuth 2 process][1]

However, if you're using this header

Authorization BoxAuth api_key={api key}&auth_token={v1 auth token}

You need to get the token through the V1 auth process, which is the one you've outlined.

As a sidenote, you should try to use the OAuth 2 process, as the V1 process is going to be deprecated later this year.

Upvotes: 1

Related Questions