使用来自Java的SignPost的Yelp V2 API - INVALID_SIGNATURE

时间:2011-01-26 16:58:08

标签: java web-services oauth signpost

尝试使用Java从Java应用程序(Android)调用v2 api SignPost oauth库。我已将我的代码解压缩为独立代码 可以从命令行运行的版本(没有依赖关系 Android的)。请看看,让我知道我做错了什么。

您可以通过输入以下命令直接运行示例:java -jar YelpV2Test.jar 我还包括一个zip文件,这是源代码,并且是必需的 库,设置为Eclipse项目。也粘贴代码 直接在下面。

Both the Executable-JAR and source ZIP file can be downloaded from my Google Docs share

我尝试从GET切换到POST,并且完全相同 结果。使用v1 API没问题。任何有关解决此问题的帮助 不胜感激!

- 代码开始 -

import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.net.MalformedURLException; 
import java.net.URLEncoder; 


import oauth.signpost.OAuthConsumer; 
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; 
import oauth.signpost.exception.OAuthCommunicationException; 
import oauth.signpost.exception.OAuthExpectationFailedException; 
import oauth.signpost.exception.OAuthMessageSignerException; 
import oauth.signpost.signature.HmacSha1MessageSigner; 
import oauth.signpost.signature.QueryStringSigningStrategy; 


import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 


public class RunMe { 
        public final String CONSUMER_KEY = "PbSjw9p08To_HZnFYqpRZg"; 
        public final String CONSUMER_SECRET = "_raqYwj6njZ15sXj4z-CYEDHiHQ"; 
        public final String TOKEN = "ARfbDucxkBi89lBHARTgygLkcMw9h8eW"; 
        public final String TOKEN_SECRET = "rzvmIKVC4WQsdZV1lM5naZ5IQ8g"; 
        public final String YWSID = "iXUUomsyqBkdXaypz7539A"; 
        public final String ENCODING_SCHEME = "UTF-8"; 

        public void start() { 
                String lat = "30.361471"; 
                String lng = "-87.164326"; 
                String category = "banks"; 
                String radius = "10"; 
                String limit = "15"; 
                String offset = "0"; 
                String sort = "0"; 
                String service = "http://api.yelp.com/v2/search"; 

                try { 
                        String query = String 
                                        .format(service 
                                                        + "?ll=%s&category_filter=%s&radius_filter=%s&limit=%s&offset= 
%s&sort=%s", 
                                                        URLEncoder.encode(lat + "," + lng, ENCODING_SCHEME), 
                                                        URLEncoder.encode(category, ENCODING_SCHEME), 
                                                        URLEncoder.encode(radius, ENCODING_SCHEME), 
                                                        URLEncoder.encode(limit, ENCODING_SCHEME), 
                                                        URLEncoder.encode(offset, ENCODING_SCHEME), 
                                                        URLEncoder.encode(sort, ENCODING_SCHEME)); 
                        OAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, 
                                        CONSUMER_SECRET); 
                        consumer.setTokenWithSecret(TOKEN, TOKEN_SECRET); 
                        consumer.setMessageSigner(new HmacSha1MessageSigner()); 
                        consumer.setSendEmptyTokens(true); 
                        consumer.setSigningStrategy(new QueryStringSigningStrategy()); 
                        String signedQuery = consumer.sign(query); 
                        System.out.println(getClass().getName() + ">> " + "Signed query: " 
                                        + signedQuery); 
                        HttpGet request = new HttpGet(signedQuery); 
                        HttpClient httpClient = new DefaultHttpClient(); 
                        HttpResponse response = (HttpResponse) httpClient.execute(request); 

                        HttpEntity entity = response.getEntity(); 
                        String result = EntityUtils.toString(entity); 
                        System.out.println(getClass().getName() + ">> " + "Result: " 
                                        + result);
                } catch (UnsupportedEncodingException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                } catch (MalformedURLException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                } catch (IOException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                } catch (OAuthMessageSignerException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                } catch (OAuthExpectationFailedException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                } catch (OAuthCommunicationException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                }
        }

        /** 
         * @param args 
         */ 
        public static void main(String[] args) { 
                // TODO Auto-generated method stub 
                new RunMe().start(); 
        }
} 

- 代码结束 -

我从这次测试中看到的结果如下:

$ java -jar YelpV2Test.jar

邵仁枚>>签名查询: http://api.yelp.com/v2/search?ll=30.361471%2C-87.164326&category_filter=banks&radius_filter=10&limit=15&offset=0&sort=0&oauth_signature=cf5C3%2BjIfTvNIQee1buzWk2%2Fz%2BE%3D&oauth_token=ARfbDucxkBi89lBHARTgygLkcMw9h8eW&oauth_consumer_key=PbSjw9p08To_HZnFYqpRZg&oauth_version=1.0&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1296021631&oauth_nonce=4831286657125973388

邵仁枚>>结果:{“error”:{“text”:“签名是 无效的“,”“id”:“INVALID_SIGNATURE”,“description”:“无效的签名。 预期的签名基本字符串:GET& http%3A%2F%2Fapi.yelp.com %2Fv2%2Fsearch&安培; category_filter%3Dbanks%26limit%3D15%26ll %3D30.361471%252C-87.164326%26oauth_consumer_key %3DPbSjw9p08To_HZnFYqpRZg%26oauth_nonce %3D4831286657125973388%26oauth_signature_method%3DHMAC- SHA1%26oauth_timestamp%3D1296021631%26oauth_token %3DARfbDucxkBi89lBHARTgygLkcMw9h8eW%26oauth_version%3D1.0%26offset %3D0%26radius_filter%3D10%26sort%3D0“}}

0 个答案:

没有答案