2017-12-01 3 views
0

oath_signatureを作成したいと思います。 は、私は私のoath_signatureが正しいSwiftでoath_signatureを作成するには?

<message>Invalid signature: oauth_signature 'd1b9eb5c8a5700e17d69894358fac6b7abe4a0ac'</message> 

ではないことを応答を得る誰かがoath_signatureを作成するために私を助けることができますか?私は多くの解決策を探したが、何も働かなかった。私は間違っているの?

私は次のように私のSignatureBaseStringを作成します。

var signatureBase: String = "" 
signatureBase += http_method + "&" 
signatureBase += normalizedUrl + " 
signatureBase += normalizedRequestParameters 
signatureBase = signatureBase.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)! 

私は次のように作成する鍵と秘密を持つ文字列:私はそのコードを生成

var stringFormat: String = 
consumer_key.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)! + 
"&" + 
consumer_secret.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)! 

署名:

signature = signatureBase.hmac(algorithm: .SHA1, key: stringFormat) 

文字列の拡張子は他の投稿のものです(CommonHMAC in Swift

私はそのコードを送信要求:

let requestUrl = normalizedUrl 
    let postString = normalizedRequestParameters + "&" + "oauth_signature=" + signature.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)! 

    //Send Request 
    var done = false 
    var request = URLRequest(url: URL(string: requestUrl)!) 
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") 
    request.httpMethod = http_method 
    request.httpBody = postString.data(using: .utf8) 
    print("Send request") 
    var requestResponse = "" 

    let task = URLSession.shared.dataTask(with: request) { data, response, error in 
     guard let data = data, error == nil else {// check for fundamental networking error 
      print("error=\(error)") 
      return 
     } 

     if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {// check for http errors 
      print("statusCode should be 200, but is \(httpStatus.statusCode)") 
      print("response = \(response)") 
     } 

     let responseString = String(data: data, encoding: .utf8) 
     done = true 
     requestResponse = responseString! 
     //print("responseString = \(responseString)") 
    } 
    task.resume() 

    //Wait for response 
    repeat { 
     RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1)) 
    } while !done 

    print(requestResponse) 

答えて

0

OAuth1が苦痛です。 OAuthSwiftと呼ばれる優れたオープンソースの実装があります。これには動作する実装があります。ここでは、彼らが何をすべきかです:

open func authorizationParameters(_ body: Data?, timestamp: String, nonce: String) -> OAuthSwift.Parameters { 
    var authorizationParameters = OAuthSwift.Parameters() 
    authorizationParameters["oauth_version"] = self.version.shortVersion 
    authorizationParameters["oauth_signature_method"] = self.version.signatureMethod.rawValue 
    authorizationParameters["oauth_consumer_key"] = self.consumerKey 
    authorizationParameters["oauth_timestamp"] = timestamp 
    authorizationParameters["oauth_nonce"] = nonce 
    if let b = body, let hash = self.version.signatureMethod.sign(data: b) { 
     authorizationParameters["oauth_body_hash"] = hash.base64EncodedString(options: []) 
    } 

    if !self.oauthToken.isEmpty { 
     authorizationParameters["oauth_token"] = self.oauthToken 
    } 
    return authorizationParameters 
} 

public enum SignatureMethod: String { 
    case HMAC_SHA1 = "HMAC-SHA1"//, RSA_SHA1 = "RSA-SHA1", PLAINTEXT = "PLAINTEXT" 

    func sign(key: Data, message: Data) -> Data? { 
     switch self { 
     case .HMAC_SHA1: 
      return HMAC.sha1(key: key, message: message) 
     } 
    } 

    func sign(data: Data) -> Data? { 
     switch self { 
     case .HMAC_SHA1: 
      let mac = SHA1(data).calculate() 
      return Data(bytes: UnsafePointer<UInt8>(mac), count: mac.count) 
     } 
    } 
} 

し、最終的に:

open func authorizationParametersWithSignature(method: OAuthSwiftHTTPRequest.Method, url: URL, parameters: OAuthSwift.Parameters, body: Data? = nil, timestamp: String, nonce: String) -> OAuthSwift.Parameters { 
    var authorizationParameters = self.authorizationParameters(body, timestamp: timestamp, nonce: nonce) 

    for (key, value) in parameters { 
     if key.hasPrefix("oauth_") { 
      authorizationParameters.updateValue(value, forKey: key) 
     } 
    } 

    let combinedParameters = authorizationParameters.join(parameters) 

    authorizationParameters["oauth_signature"] = self.signature(method: method, url: url, parameters: combinedParameters) 

    return authorizationParameters 
} 

私はあなたのコードと彼らを実行すると、あなたのエラーが存在する場所を決定するために比較することをお勧めしたいです。あなたが間違ったものに署名しているか、最後にoauth_body_hashを追加していない可能性があります。

+0

あなたの答えをありがとう、私はそれを確認します。私は自分の問題が署名の最後の生成だと思う。 – MMbach

関連する問題