【问题标题】:Cannot properly sign Twitter OAuth 1 request无法正确签署 Twitter OAuth 1 请求
【发布时间】:2013-09-03 08:22:31
【问题描述】:

我正在尝试签署 OAuth 1 令牌请求,具体来说:

https://api.twitter.com/oauth/request_token

GitHub Gist HERE,更易阅读:

  def timestamp: String = (System.currentTimeMillis / 1000).toString;
  def nonce: String = System.nanoTime.toString

  /**
   * Generates the OAuth 1 header for a request.
   */
  def oauthHeader(
    endPoint: String,
    requestMethod: String,
    consumerKey: String,
    callback: String,
    params: List[(String, String)] = List()): String = {
    val signature = sign(endPoint, requestMethod, consumerKey);
    val oauthParams: List[(String, String)] = params :::
      (OAuthParams.oauth_consumer_key -> consumerKey) ::
      (OAuthParams.oauth_callback -> URLEncoder.encode(callback, UTF_8)) ::
      (OAuthParams.oauth_nonce -> nonce) ::
      (OAuthParams.oauth_timestamp -> timestamp) ::
      (OAuthParams.oauth_signature -> signature) ::
      (OAuthParams.oauth_signature_method -> "HMAC-SHA1") ::
      (OAuthParams.oauth_version -> "1.0") :: Nil

    val encodedParams = oauthParams.sortBy(_._1) map (header => {
      header._1 + "=" + "\"" + header._2 + "\""
    })

    OAuthParams.oauth + " " + encodedParams.mkString(", ")
  }

  def sign(endPoint: String,
    requestMethod: String,
    consumerKey: String,
    params: List[(String, String)] = List()): String = {

    /**
     * The map of OAuth params.
     * Added to every request.
     */
    val oauthParams: List[(String, String)] = params :::
      (OAuthParams.oauth_consumer_key -> consumerKey) ::
      (OAuthParams.oauth_nonce -> nonce) ::
      (OAuthParams.oauth_timestamp -> timestamp) ::
      (OAuthParams.oauth_signature_method -> "HMAC-SHA1") ::
      (OAuthParams.oauth_version -> "1.0") ::
      Nil

    /**
     * Percent encoded List of parameters.
     */
    val signatureMap = oauthParams.sortBy(_._1).map(header => {
      URLEncoder.encode(header._1 + "=" + header._2, UTF_8)
    });

    /**
     * Add the request method.
     */
    val encodedHeader = requestMethod ::
      URLEncoder.encode(endPoint, UTF_8) ::
      URLEncoder.encode(oauthParams.mkString("&"), UTF_8) :: Nil

    URLEncoder.encode(hmacSha1(encodedHeader.mkString("&"), signinKey), UTF_8);
  }

  def hmacSha1(value: String, key: String): String = {
    val keyBytes = key.getBytes();
    val signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
    val mac = Mac.getInstance("HmacSHA1");
    mac.init(signingKey);
    val rawHmac = mac.doFinal(value.getBytes());
    Base64.encodeBase64String(rawHmac)
  }

我不断收到:无法验证 oauth 签名和令牌。

提供:

  • 应用程序已注册
  • 在应用设置中启用了“使用 Twitter 登录”

我做错了什么?

【问题讨论】:

    标签: java scala oauth twitter-oauth


    【解决方案1】:

    我看到的第一个问题是随机数和时间戳的生成。这两种方法都有可能在每次调用时产生不同的值。如果它们在签名而不是标头中具有不同的值,那么它们将导致服务器生成与您提供的签名不匹配的不同签名。

    您是否考虑过使用库?

    我知道DispatchPlay WS API 都提供对 OAuth 请求的支持。我相信玩!使用 Signpost 并且我也很幸运地使用了来自 OAuth 网站 http://oauth.googlecode.com/svn/code/java/ 的这段代码的 Java

    【讨论】:

      【解决方案2】:

      我设法推出了自己的 OAuth 1.0a 请求签名者。

      这是其中的 HMAC-SHA1 部分。

      https://gist.github.com/alexflav23/6407319

      完整的实现将在 net.databinder.dispatch 拉取请求中进行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-05
        • 1970-01-01
        • 1970-01-01
        • 2013-05-13
        • 1970-01-01
        • 1970-01-01
        • 2013-01-14
        • 2016-10-23
        相关资源
        最近更新 更多