使用Alamofire进行OAuth1.0身份验证

时间:2016-11-09 15:30:58

标签: swift3 alamofire oauth-1.0a

我在swift3.0项目中使用alamofire进行网络连接。我需要从woocommerce rest apis获取数据,例如http://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties 下面是我在项目中添加的代码。我认为存在身份验证问题。

let params = ["oauth_consumer_key":consumerKey, "oauth_consumer_secret":consumerSecret, "oauth_timestamp":timeInterval, "oauth_nonce": nonce, "oauth_signature_method": "HMAC-SHA1", "oauth_version": "1.0"] as [String : Any];

    Alamofire.request(url, parameters: params)
        .responseJSON { response in
            print(response.request)  // original URL request
            print(response.response) // HTTP URL response
            print(response.data)     // server data
            print(response.result)   // result of response serialization

            if let JSON = response.result.value {
                print("JSON: \(JSON)")
            }
    }

响应:

{
    code = "woocommerce_rest_cannot_view";
    data =     {
        status = 401;
    };
    message = "Sorry, you cannot view this resource.";
}

0 个答案:

没有答案