【问题标题】:Oauth verification errorOauth 验证错误
【发布时间】:2012-05-07 13:53:18
【问题描述】:

我正在尝试使用 Oauth 进行验证。我的示例代码是:

ArrayList <NameValuePair> params = new ArrayList<NameValuePair>();
  boolean flag=false;
  URL url;

  String oauth_nonce="q14F5ApN4Ka5RJGbYgabXwMSZ4BxVrlNGedN9zoFAoi3rc7dpLjIgri1erAAriPu";
  String oauth_timestamp="1336069726";
  String signatureString =
    URLEncoder.encode("device_id=id_here")
    +URLEncoder.encode("&device_type=psw_here")
    +URLEncoder.encode("&oauth_consumer_key=consumer_key_here...&oauth_nonce=" +
      oauth_nonce+
      "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1336069726&oauth_version=1.0");

  String percent_encoded_url = URLEncoder.encode("http://www.example.com");
  String signatureBaseString = "POST"+
    "&"+percent_encoded_url+
    "&"+signatureString;

  String oauth_signature=URLEncoder.encode(computeHmac(signatureBaseString, "aWAQTWR7VvHapaNhWBCL0VaUdGN2xzN4&"));

  String headerValue ="OAuth "+
    "oauth_consumer_key=\""+"consumer_key_here..."+"\","+
    "oauth_signature_method=\"HMAC-SHA1"+"\","+
    "oauth_signature=\""+oauth_signature+"\","+
    "oauth_timestamp=\"1336069726"+"\","+
    "oauth_nonce=\""+oauth_nonce+"\"," +        
    "oauth_version=\"1.0"+"\"";


  HttpPost httppost = new HttpPost("http://www.example.com/");

  httppost.setHeader("Content-Type","application/x-www-form-urlencoded");
  httppost.setHeader("Authorization",headerValue);
  HttpEntity httpentity = new UrlEncodedFormEntity(params);

  HttpClient httpclient = new DefaultHttpClient(clientConnectionManager, httpparams);

  StringBuilder sb = new StringBuilder();
  try {

   HttpResponse response = httpclient.execute(httppost);

当我发送请求时,它总是说“签名匹配错误”。我对Oauth一无所知。我不知道我错在哪里。有人可以指导我正确的方向吗?

【问题讨论】:

    标签: android oauth verification


    【解决方案1】:

    在我看来,您正在以正确的方式生成签名,但服务器使用通用 signature mismatch 消息进行响应,因为:

    首先,您不能对每个请求使用相同的随机数。您需要为每个请求随机生成一个新的 nonce。

    第二,对于每个请求,时间戳都需要是 UTC 的当前时间。如果正确实施 OAuth 提供程序,它将允许在不久的将来或过去的时间戳。所以请确保你的时钟是正确的。

    据我所知,您的签名生成看起来不错,至少在您尝试获取请求令牌时是这样。试试上面的两点,让我知道它是怎么回事:)

    【讨论】:

      猜你喜欢
      • 2016-08-14
      • 2015-08-23
      • 2014-06-07
      • 2018-04-26
      • 2016-03-27
      • 1970-01-01
      • 2016-11-14
      • 2015-04-28
      相关资源
      最近更新 更多