无法在Java中使用OAuth2和HTTP从Mendeley检索数据
我有一个应用程序,在门捷利的OAuth1上运行良好。由于OAth1不再受支持,我必须将我的应用程序迁移到OAuth2以获取数据 我得到了令牌响应,但是我不能请求任何内容,程序抛出一个NullPointerException 我正在测试这个源代码 我还使用apacheoltu和apachehttpclient 这是我尝试运行的代码:无法在Java中使用OAuth2和HTTP从Mendeley检索数据,java,mendeley,Java,Mendeley,我有一个应用程序,在门捷利的OAuth1上运行良好。由于OAth1不再受支持,我必须将我的应用程序迁移到OAuth2以获取数据 我得到了令牌响应,但是我不能请求任何内容,程序抛出一个NullPointerException 我正在测试这个源代码 我还使用apacheoltu和apachehttpclient 这是我尝试运行的代码: static OAuthClientRequest request; static OAuthClient oAuthClient; static OAuthJSO
static OAuthClientRequest request;
static OAuthClient oAuthClient;
static OAuthJSONAccessTokenResponse tokenResponse ;
static String CATALOG_URL="https://api-oauth2.mendeley.com/oapi/documents/groups?items=10";
request = OAuthClientRequest
.tokenLocation("https://api-oauth2.mendeley.com/oauth/token")
.setClientId(Client_ID)
.setClientSecret(Secret)
.setGrantType(GrantType.CLIENT_CREDENTIALS)
.setScope("all")
.buildBodyMessage();
System.out.println("is set up");
oAuthClient = new OAuthClient(new URLConnectionClient());
tokenResponse = oAuthClient.accessToken( request, OAuthJSONAccessTokenResponse.class);
System.out.println("token is retrieved");
HttpGet httpGet = new HttpGet(CATALOG_URL);
httpGet.setHeader("Authorization", "Bearer " + tokenResponse.getAccessToken());
//this is where the Exception is thrown
HttpResponse httpResponse = apacheHttpClient.execute(httpGet);
//
System.out.println("this is it: "+httpResponse.toString());
String responseAsString = EntityUtils.toString(httpResponse.getEntity());
System.out.println(responseAsString);
我得到的例外是:
Exception in thread "main" java.lang.NullPointerException
我现在问自己为什么会这样。
目录URL来自Mendeley网站,应该返回包含所有公共组的列表的第一页
我还尝试了门捷利网站的不同URL
HttpGet语句可能有什么问题吗
有人有任何提示吗?您正在接收NullPointerException,因为您使用的变量(apacheHttpClient)尚未初始化。试着先做这个
apacheHttpClient = ApacheHttpTransport.newDefaultHttpClient();