栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何调用API(Oauth 1.0)?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何调用API(Oauth 1.0)?

PFB工作代码。希望它会有用。

import java.io.InputStream;import java.io.StringWriter;import java.net.URI;import java.net.URISyntaxException;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.AuthorizationHeaderSigningStrategy;import org.apache.commons.io.IOUtils;import org.apache.http.HttpHost;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpRequestbase;import org.apache.http.impl.client.DefaultHttpClient;import com.intuit.ipp.data.Account;import com.intuit.ipp.exception.FMSException;import com.intuit.ipp.net.MethodType;import com.intuit.ipp.services.DataService;public class POCWithoutDevkitTest {    private DataService service;    private OAuthConsumer oAuthConsumer;    private static String realmID = null;    public POCWithoutDevkitTest() {        realmID = "122294642099";        String consumerKey = "AAAAA";        String consumerSecret = "BBBBB";        String accessToken = "CCCCC";        String accessTokenSecret = "DDDDD";        setupContext(consumerKey, consumerSecret, accessToken, accessTokenSecret);    }    public void setupContext(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret) { this.oAuthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret); oAuthConsumer.setTokenWithSecret(accessToken, accessTokenSecret); oAuthConsumer.setSigningStrategy(new AuthorizationHeaderSigningStrategy());    }    public void authorize(HttpRequestbase httpRequest) throws FMSException {        try { oAuthConsumer.sign(httpRequest);        } catch (OAuthMessageSignerException e) { throw new FMSException(e);        } catch (OAuthExpectationFailedException e) { throw new FMSException(e);        } catch (OAuthCommunicationException e) { throw new FMSException(e);        }    }    public void executeGetRequest(String customURIString){        DefaultHttpClient client = new DefaultHttpClient();        client.getParams().setParameter("http.protocol.content-charset", "UTF-8");        HttpRequestbase httpRequest = null;        URI uri = null;        try { uri = new URI(customURIString);        } catch (URISyntaxException e) { e.printStackTrace();        }        String methodtype = "GET";        if (methodtype.equals(MethodType.GET.toString())) { httpRequest = new HttpGet(uri);        }        httpRequest.addHeader("content-type", "application/xml");        httpRequest.addHeader("Accept","application/xml");        try { authorize(httpRequest);        } catch (FMSException e) { e.printStackTrace();        }        HttpResponse httpResponse = null;        try { HttpHost target = new HttpHost(uri.getHost(), -1, uri.getScheme()); httpResponse = client.execute(target, httpRequest); System.out.println("Connection status : " + httpResponse.getStatusLine()); InputStream inputStraem = httpResponse.getEntity().getContent(); StringWriter writer = new StringWriter(); IOUtils.copy(inputStraem, writer, "UTF-8"); String output = writer.toString(); System.out.println(output);        }catch(Exception e){ e.printStackTrace();        }    }    public static void main(String args[]) {        POCWithoutDevkitTest withoutDevkitClient = new POCWithoutDevkitTest();        withoutDevkitClient.executeGetRequest("https://appcenter.intuit.com/api/v1/connection/reconnect");    }}

依存关系

    <dependency>        <groupId>org.apache.httpcomponents</groupId>        <artifactId>httpcore</artifactId>        <version>4.3.1</version>    </dependency>    <dependency>        <groupId>oauth.signpost</groupId>        <artifactId>signpost-core</artifactId>        <version>1.2.1.1</version>    </dependency>    <dependency>        <groupId>oauth.signpost</groupId>        <artifactId>signpost-commonshttp4</artifactId>        <version>1.2</version>    </dependency>    <dependency>        <groupId>org.apache.commons</groupId>        <artifactId>commons-io</artifactId>        <version>1.3.2</version>    </dependency>    <dependency>        <groupId>org.apache.httpcomponents</groupId>        <artifactId>httpmime</artifactId>        <version>4.3.1</version>    </dependency>

谢谢



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495716.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号