0

我有一个与 Mendeley 上的 OAuth1 配合得很好的应用程序。由于不再支持 OAth1,我必须将我的应用程序迁移到 OAuth2 以获取数据。

我得到了令牌响应,但我无法请求任何内容,程序抛出 NullPointerException。

我在这里测试这个源代码。

我还使用 Apache OLTU 和 Apache HTTPClient

这是我尝试运行的代码:

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

我现在问自己为什么会这样。CATALOG_URL 来自 Mendeley 网站,应该返回包含所有公共组的列表的第一页。

我还尝试了与 Mendeley 网站不同的 URL。

HttpGet语句有什么问题吗?

有没有人有任何提示?

4

1 回答 1

2

您收到 NullPointerException 是因为您正在使用尚未初始化的变量 (apacheHttpClient)。尝试先这样做

apacheHttpClient = ApacheHttpTransport.newDefaultHttpClient();
于 2014-06-16T14:47:13.053 回答