How to call API (Oauth 1.0)?

前端 未结 3 1258
[愿得一人]
[愿得一人] 2020-12-03 16:23

I am trying to call this API (Oauth1.0 standard):

https://appcenter.intuit.com/api/v1/Connection/Reconnect

And what I am doing is : (It am working on java)

3条回答
  •  醉酒成梦
    2020-12-03 16:55

    This is the working HTTP GET requestcode in java when all keys are in hand. Hope it does your work.

    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    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 org.apache.http.*;
    import org.apache.http.client.*;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.client.methods.*;  //HttpHead, HttpPut, HttpGet, etc...
    import org.apache.http.util.EntityUtils;
    
    public class HttpGetRequest {
    
    public static void demo() throws IOException {           
        String consumer_key = "bcd";
        String consumer_secret = "efg";
        String access_token = "rst";
        String access_secret= "xzy";
    
        OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumer_key,
    consumer_secret);
        consumer.setTokenWithSecret(access_token, access_secret);
    
    
        String uri = "your url here";
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(uri);
        try {
            consumer.sign(httpget);
        } catch (OAuthMessageSignerException ex) {
            Logger.getLogger(HttpRequest.class.getName()).log(Level.SEVERE,       null, ex);
        } catch (OAuthExpectationFailedException ex) {
            Logger.getLogger(HttpRequest.class.getName()).log(Level.SEVERE, null, ex);
        } catch (OAuthCommunicationException ex) {
            Logger.getLogger(HttpRequest.class.getName()).log(Level.SEVERE, null, ex);
        }
        HttpResponse response = httpclient.execute(httpget);
        System.out.println(response.getStatusLine().toString());
        HttpEntity entity = response.getEntity();
        System.out.println();
        System.out.println(EntityUtils.toString(entity));       
    }
    public static void main(String[] args) {
        try {
            demo();
        }
        catch(IOException ioe) {
            System.out.println(ioe);
        }
    }
    }
    

提交回复
热议问题