Search code examples
objective-cuber-api

uber api ride request getting 401 unautherized error response


I am trying to build mac osx application For UBER.I have completed all steps of Aouth2. I am successfully getting access_token. I am also able to retrieve user profile and history but while I am trying to post "ride request", I am getting 401 unauthorized error response.please help.Thank you In advance. My code is as below.

**

//POST /v1/requests
    NSString *sandBoxURL=@"https://sandbox-api.uber.com/v1";
    NSString *url = [NSString stringWithFormat:@"%@/requests", sandBoxURL];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    [request addValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:[NSString stringWithFormat:@"Bearer %@", _accessToken] forHTTPHeaderField:@"Authorization"];

    NSError *error = nil;
    request.HTTPMethod = @"POST";
    request.HTTPBody = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
    NSLog(@"Request for Product Request:%@",request);
    [self performNetworkOperationWithRequest:request completionHandler:^(NSDictionary *requestDictionary, NSURLResponse *response, NSError *error)
     {
         NSLog(@"Response for Product Request:%@",response);
         NSLog(@"Result:%@",requestDictionary);
         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
         if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300)
         { //OK
             UberRequest *requestResult = [[UberRequest alloc] initWithDictionary:requestDictionary];
             // handler(requestResult, response, error);
         }
         if (409 == httpResponse.statusCode) { //needs surge confirmation
             NSLog(@"Surge Conflict");
         }
         else
         {
            NSLog(@"Error In response");

         }
     }];

** And response I am getting is:

    { URL: https://sandbox-api.uber.com/v1/requests } { status code: 401, headers {
    Connection = "keep-alive";
    "Content-Length" = 83;
    "Content-Type" = "application/json";
    Date = "Mon, 07 Nov 2016 11:19:35 GMT";
    Server = nginx;
    "Strict-Transport-Security" = "max-age=0";
    "X-Content-Type-Options" = nosniff;
    "X-Uber-App" = "uberex-sandbox, migrator-uberex-sandbox-optimus";
    "X-Uber-Missing-Scopes" = true;
    "X-XSS-Protection" = "1; mode=block";
} }
Result:{
    code = unauthorized;
    message = "Requires at least one scope. Available scopes: ";
}

Solution

  • I got the solution. Problem Was With My Scope input.While requesting for token,even if the account you are login is of developer, we need put scopes properly. In my case I needed request scope in token.