我在尝试使用alamofire和OAuth1对请求进行身份验证时遇到问题,并且正在使用此库来这样做: https://github.com/phenemann/Authentication/ 但是如果您知道如何将Alamofire与OAuth1结合使用,我将无法成功。
宝贝,直到现在我的进步
class HttpRestManager{
public static func sendRequest(url: String, authorizationType: AuthorizationType, method: HTTPMethod, parameters: [String: Any]?, headers: NSMutableDictionary, complition: @escaping (_ results: String?, _ errors: Error?, _ statusCode: Int?) -> Void){
let preparedHeaders = prepareHeaders(headers: headers, authorizationType: authorizationType)
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 10
manager.adapter = AuthorizationAdapter()
manager.request(url, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: preparedHeaders).responseString { (response) in
switch response.result{
case .success(let value):
complition(value, nil, response.response?.statusCode)
case .failure(let error):
complition(nil, error, response.response?.statusCode)
}
}
}
}
这是我正在使用的适配器:
class AuthorizationAdapter: RequestAdapter {
private let CONSUMER_KEY = "****";
private let CONSUMER_SECRET = "****";
private let TOKEN_ID = "****";
private let TOKEN_SECRET = "****";
func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
let oAuthMethod = AuthenticationMethod.oauth1(consumerKey: CONSUMER_KEY,
consumerSecret: CONSUMER_SECRET,
accessKey: TOKEN_ID,
accessSecret: TOKEN_SECRET)
let authorizer = Authenticator(method: oAuthMethod)
let authorizedRequest = try? authorizer.signRequest(request: urlRequest)
return authorizedRequest!
}
}
请注意,我有ConsumerKey,ConsumerSecret,TokenID和TokenSecret,没有CallBackUrl或我只需要根据请求URL及其参数生成“ oauth_signiture”值,就已经生成了,但是响应我得到的是“ INVALID_LOGIN_ATTEMPT”,每当我在Postman上测试API时,即使生成的标头类似于我生成的标头,它也会成功,请帮助我或告诉我我做错了,非常感谢。