2016-07-25 5 views
1

現在、IamはJavaとライブラリnet.oauthを使用してMendeleyを認証しようとしています。私の目標は、Mendeleyからの読者データを検索して、それらを学術文書のデータベースに追加することです。 net.oauthでOAuth2を使用してMendeleyを認証する方法

net.oauth.OAuthProblemException net.oauth.client.OAuthClient.invoke(OAuthClient.java:246)で :

は、残念ながら私は現在、401と、次の例外を取得しています.client.OAuthClient.invoke(OAuthClient.java:143) at net.oauth.client.OAuthClient.getRequestToken(OAuthClient.java:101) at net.oauth.client.OAuthClient.getRequestToken(OAuthClient.java:77) net.oauth.client.OAuthClient.getRequestToken(OAuthClient.java:116) at org.mrdlib.mendeleyCrawler.mendeleyConnection.defaultClien t(mendeleyConnection.java:82) at org.mrdlib.mendeleyCrawler.mendeleyConnection.getReadership(mendeleyConnection.java:124) at org.mrdlib.mendeleyCrawler.mendeleyConnection.main(mendeleyConnection.java:190) at sun.reflect。 java.lang.reflect.Method.invokeでsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) でsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) でNativeMethodAccessorImpl.invoke0(ネイティブメソッド) org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.mainで(Method.java:497) (JarRsrcLoader.java:58)

Iを使用次のコードは:

public class mendeleyConnection { 

private OAuthAccessor client; 
private String access_token; 
private String request_token; 
private DBConnection con; 

public mendeleyConnection() { 
    con = new DBConnection(); 
} 

public String convertToAccessToken(String request_token) { 
    ArrayList<Map.Entry<String, String>> params = new ArrayList<Map.Entry<String, String>>(); 
    OAuthClient oclient = new OAuthClient(new HttpClient4()); 
    OAuthAccessor accessor = client; 
    params.add(new OAuth.Parameter("oauth_token", request_token)); 
    try { 
     OAuthMessage omessage = oclient.invoke(accessor, "POST", accessor.consumer.serviceProvider.accessTokenURL, 
       params); 
     return omessage.getParameter("oauth_token"); 
    } catch (OAuthProblemException e) { 
     e.printStackTrace(); 
     return ""; 
    } catch (Exception ioe) { 
     ioe.printStackTrace(); 
     return ""; 
    } 
} 

public OAuthAccessor defaultClient() { 
    String callbackUrl = "some fallback url"; 
    String consumerKey = "the id of the mendeley application"; 
    String consumerSecret = "a generated secret"; 
    String reqUrl = "https://www.mendeley.com/oauth/request_token/"; 
    String authzUrl = "https://api-oauth2.mendeley.com/oauth/authorize/"; 
    String accessUrl = "https://www.mendeley.com/oauth/access_token/"; 
    OAuthServiceProvider provider = new OAuthServiceProvider(reqUrl, authzUrl, accessUrl); 
    OAuthConsumer consumer = new OAuthConsumer(callbackUrl, consumerKey, consumerSecret, provider); 
    OAuthAccessor accessor = new OAuthAccessor(consumer); 

    OAuthClient oaclient = new OAuthClient(new HttpClient4()); 

    try { 
     oaclient.getRequestToken(accessor); 
     request_token = accessor.requestToken; 
    } catch (OAuthProblemException e) { 
     e.printStackTrace(); 
     System.out.println(e.getHttpStatusCode()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return accessor; 
} 

public HashMap<String, Readership> getReadership() { 
    HashMap<String, Readership> map = new HashMap<String, Readership>(); 
    List<String> documentTitles = new ArrayList<>(); 
    Readership readership = null; 
    String mendeleyId = null; 
    int score = 0; 
    HttpPost httppost = new HttpPost(); 
    URL url = null; 
    String nullFragment = null; 
    JSONObject jsonObject = null; 

    documentTitles = con.getAllDocumentTitles(); 

    for (int i = 0; i < documentTitles.size(); i++) { 
     String current = documentTitles.get(i); 

     HttpClient httpclient = HttpClientBuilder.create().build(); 
     String urlString = "https://api.mendeley.com/catalog?title=" + current; 

     client = defaultClient(); 
     access_token = convertToAccessToken(client.requestToken); 

     try { 
      url = new URL(urlString); 
      URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), nullFragment); 
      httppost = new HttpPost(uri); 
      httppost.addHeader("Authorization", "Bearer " + client.requestToken); 


      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("action", "getjson")); 

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      String data = EntityUtils.toString(response.getEntity()); 

      jsonObject = (JSONObject) JSONValue.parse(data); 
      mendeleyId = (String) jsonObject.get("id"); 
      score = (Integer) jsonObject.get("score"); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     [...] 
    } 
    return map; 
} 

public static void main(String[] args) { 
    mendeleyConnection mcon = new mendeleyConnection(); 
    mcon.getReadership(); 

} 

} 

例外

oaclient.getRequestToken(アクセッサ)でスローされます。

私はHttp Requests and Authentificationのトピックで経験がないので、私はいくつかの助けに感謝します。私はMendeleyのガイドとインターネット上で見られるすべての例を読んだ。私はGetリクエストも使用しましたが、これはうまくいきませんでした。私はMendeleyからURLを変更しました(ドキュメントには異なるものがあり、動作しません)。私は別の例を試しました。私はGoogleからAPIを試してみましたが、これは純粋なOverkillでした。私は一緒に例をつけることさえできませんでした。私は現在、私のURLが間違っているかもしれないと推測しています。なぜなら、メソッド "defaultClient"の例が何回か正確に見つかったからです。 OAuth2に変更があったかもしれませんか?

ありがとうございました!

答えて

0

URLは間違っています。

文字列reqUrl = "https://www.mendeley.com/oauth/request_token/";

文字列authzUrl = "https://api-oauth2.mendeley.com/oauth/authorize/";

文字列accessUrl = "https://www.mendeley.com/oauth/access_token/";ここで

はあなたが役に立つかもしれません私たちの開発者ポータルから認証コードフローに関するドキュメントである - http://dev.mendeley.com/reference/topics/authorization_auth_code.html

+0

うん、私はすでに提案厥。しかし、働いているものは何ですか?私もString reqUrl = "https://www.mendeley"を試しました。com/oauth/token/"; 文字列authzUrl =" https://api.mendeley.com/oauth/authorize/ "; 文字列accessUrl =" https://www.mendeley.com/oauth/token/ ";その他 – Millah

+0

Authorize URL = https://api.mendeley.com/oauth/authorizeトークンURLはhttps://api.mendeley.com/oauth/tokenです。 – MendeleyStack

関連する問題