OAuth with Android

I am trying to access the Yelp API. I got my keys and read about 40 articles explaining what I need to do, and I tried everything. The key is that which is not valid. I get the following:

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

I imported the jar commons-codec-1.6, signpost-commonshttp4-1.2.1.2.jar and 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;
  }
}
+3
source share
2 answers

Go to Project → Properties and on the Java Build Path → Order and Export tab make sure that these JARs are checked.

And for future reference only, if these are your real keys, I would put them on a non -public website. Just say.

+5

Android. signpost-core-1.2.1.2, CommonsHttpOAuthConsumer. http://www.java2s.com/Code/Jar/s/Downloadsignpostcommonshttp41211jar.htm

.

0

All Articles