Search code examples
swiftapialamofiremapmyfitness

Error 403 on MapMyFitness API request


i'n trying to do some request on the MapMyFitness API (Oauth2).

I can get my Authorization Code with the oauth2 identification, but now i need to request to some route.

Here is the request :

Alamofire.request(self.oauthClient.baseURL() + "workout/",
                      method: .get,
                      parameters: nil,
                      headers: authorizationHeader
                   )
        .validate()
        .responseData { [weak self] (response) in
            switch response.result {
            case .success(let data):
                do {
                    let jsonResponse = try JSON(data: data)            
                    success(items);
                    self?.isLoading = false;
                } catch {
                    self?.isLoading = false;
                    failed(nil)
                }
                break;
            case .failure(let error):
                print(error);
                self?.isLoading = false;
                failed(error)
                break;
            }
    }

In the header here is the authorization code like this :

"Authorization": "Bearer ***********************************"

Here is the doc about the route API : https://developer.underarmour.com/docs/v71_Workout

Alamofire error:

responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(403))

Am i missing something ? Thx


Solution

  • Problem founded !

    If you are using MapMyFitness API you have to use https://www.mapmyfitness.com/v7.1/ url for the authentification but when you need to request route it's https://api.ua.com/v7.1/.

    Moreover, you need to add in your header the "api-key".

    ["Authorization": "Bearer ***********************************", "api-key": client_id]
    

    and you need to set an user_id in params.

    var params = ["user" : self.userID, field_set: "time_series"] as [String : Any]
    

    These informations are not in the documentation.

    Here is my final request :

    self.authorizationHeader.updateValue(self.clientID, forKey: "api-key")
    var params = ["field_set" : "time_series",
                  "user" : self.userID] as [String : Any]
    
     Alamofire.request(self.url + "/workout/",
                              method: .get,
                              parameters: params,
                              headers: self.authorizationHeader
                )
                .validate()
                .responseData { [weak self] (response) in
                    switch response.result {
                    case .success(let data):
                        do {
                            let jsonResponse = try JSON(data: data)
                            success(items);
                        } catch {
                            failed(nil)
                        }
                        break;
                    case .failure(let error):
                        print(error);
                        failed(error)
                        break;
                    }
            }
        }