OAuth with Android

半城伤御伤魂 提交于 2019-12-10 22:03:41

问题


I am attempting to access the Yelp API. I have obtained my keys and have read about 40 articles explaining what I need to do and I have attempted everything. The key and what not are valid. I am getting the following:

05-16 17:39:54.955: E/AndroidRuntime(538): java.lang.NoClassDefFoundError: oauth.signpost.commonshttp.CommonsHttpOAuthConsumer

I have imported the commons-codec-1.6 jar, the signpost-commonshttp4-1.2.1.2.jar, and the signpost-core-1.2.1.2.jar.

I am still getting the error message. Any help?

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;


/**
 * Example for accessing the Yelp API.
 */
public class Yelp {
  private String consumerKey = "";
  private String consumerSecret = "";
  private String token = "";
  private String tokenSecret = "";

  /**
   * Search with term and location.
   *
   * @param term Search term
   * @param latitude Latitude
   * @param longitude Longitude
   * @return JSON string response
   */
  public String search(String term, double latitude, double longitude) throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException {



    try {
        OAuthConsumer myConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
        myConsumer.setTokenWithSecret(token, tokenSecret);

        String urlToPost = "http://api.yelp.com/v2/search";
        Map<String, String> myMap = new HashMap<String, String>();
        myMap.put(OAuth.OAUTH_CONSUMER_KEY, consumerKey);
        myMap.put(OAuth.OAUTH_TOKEN, token);
        myMap.put(OAuth.OAUTH_SIGNATURE_METHOD, "HMAC-SHA1");
        myMap.put(OAuth.OAUTH_SIGNATURE, tokenSecret);
        myMap.put(OAuth.OAUTH_TIMESTAMP, Long.toString(System.currentTimeMillis() / 1000L));
        myMap.put(OAuth.OAUTH_NONCE, "asdf");

        urlToPost = OAuth.addQueryParameters(urlToPost, myMap);

        HttpURLConnection myCon = (HttpURLConnection) (new URL(urlToPost)).openConnection();
        myConsumer.sign(myCon);

        myCon.connect();
        return myCon.getResponseMessage();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
  }
}

回答1:


Go to Project -> Properties, and under your Java Build Path -> Order and Export tab make sure those JARs are checked.

And, just for future reference, if those are your real keys, I would make sure to not put them on a public website. Just sayin.




回答2:


I also face same problem in Android app. when i use signpost-core-1.2.1.2 but It was not able to resolve CommonsHttpOAuthConsumer . I download it from http://www.java2s.com/Code/Jar/s/Downloadsignpostcommonshttp41211jar.htm

It worked.



来源:https://stackoverflow.com/questions/10623864/oauth-with-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!