Regarding accessing digg api

163 views
Skip to first unread message

Gaurav Khatwani

unread,
Apr 23, 2012, 2:56:35 AM4/23/12
to digga...@googlegroups.com
I have written the following code.
Bad Request{
    "status": 400, 
    "timestamp": 1335163724, 
    "message": "Invalid OAuth request - no user", 
    "code": 5005
}
Please let me know what mistake i am comitting.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;



public class Main {




    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException  {
//        HttpRequestAdapter h=new HttpRequestAdapter(connection);
  System.setProperty("http.proxyHost", "10.66.184.116");
System.setProperty("http.proxyPort", "80");

        HttpHost proxy = new HttpHost("10.66.184.116", 8080, "http");
        HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
  try{
        OAuthConsumer consumer = new DefaultOAuthConsumer("api key","secret key");
        System.out.println("Fetching request token from Digg...");

        String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);

        System.out.println("Request token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());
   
        System.out.println("Now visit:\n" + authUrl
                + "\n... and grant this app authorization");
        System.out.println("Enter the PIN code and hit ENTER when you're done:");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String pin = br.readLine();

        System.out.println("Fetching access token from Digg...");

        provider.retrieveAccessToken(consumer, pin);

        String access_token=consumer.getToken();
        String token_secret=consumer.getTokenSecret();
                consumer.setTokenWithSecret(access_token, token_secret);
        HttpURLConnection request = (HttpURLConnection) url.openConnection();

        // sign the request
        consumer.sign(request);
        request.connect();

 System.out.print(request.getResponseMessage());

HttpPost post=new HttpPost("http://services.digg.com/2.0/story.digg");
 
   HttpResponse response = (HttpResponse) httpclient.execute(post);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("oauth_consumer_key", consumer.getConsumerKey()));

nameValuePairs.add(new BasicNameValuePair("oauth_token", consumer.getToken()));
nameValuePairs.add(new BasicNameValuePair("oauth_token_secret",consumer.getTokenSecret()));
nameValuePairs.add(new BasicNameValuePair("story_id", "20100719170232:22720024"));

post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));

String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
if (line.startsWith("Auth=")) {
String key = line.substring(5);
// Do something with the key
}

}
        }
            catch (Exception e) {
            e.printStackTrace();
        }
      }

    }

Dave Beckett

unread,
Apr 23, 2012, 12:47:56 PM4/23/12
to Digg API Developers Support
That's because the oauth request is invalid in some way. From a skim
of your code, I see you are adding query parameters after doing oauth
work - that will break the signature. You should follow the oauth
library API and never adjust the request parameters - most oauth apis
I've used have wrappers that sign and then call the request for you.
This is the standard oauth approach. It's all about signing HTTP
requests.

Dave

On Apr 22, 11:56 pm, Gaurav Khatwani <khatwanigaurav...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages