2016-12-07 82 views
0

我正在嘗試爲我的應用生成OAuth訪問令牌以從Evernote獲取筆記。下面是Java代碼它使用抄寫員檢索OAuth訪問令牌時出現異常

public class EverNoteOauthGenerator { 
    public static void main(String[] args) throws EDAMUserException, EDAMSystemException, TException { 
     Class<? extends EvernoteApi> providerClass = EvernoteApi.Sandbox.class; 
     /** 
     if (EVERNOTE_SERVICE == EvernoteService.PRODUCTION) { 
      providerClass = org.scribe.builder.api.EvernoteApi.class; 
     } **/ 

     // Generate Request token & Request secret 
     String CONSUMER_KEY = "******"; 
     String CONSUMER_SECRET = "******"; 
     String cbUrl = "https://sandbox.evernote.com/OAuth.action?oauth_token=****.158D9C1C696.6C6F63616C686F7374.089C889A2F3D722B9ECC231961A3BF49"; 
     OAuthService service = new ServiceBuilder() 
      .provider(providerClass) 
      .apiKey(CONSUMER_KEY) 
      .apiSecret(CONSUMER_SECRET) 
      .callback(cbUrl) 
      .build(); 

     Token scribeRequestToken = service.getRequestToken(); 
     //String requestToken = scribeRequestToken.getToken(); 
     //String requestTokenSecret = scribeRequestToken.getSecret(); 

     String authUrl = EvernoteService.SANDBOX.getAuthorizationUrl(scribeRequestToken.getToken()); 

     // Generate OAuth Access token 
     Token scribeRequestToken = new Token(requestToken, requestTokenSecret); 

     Verifier scribeVerifier = new Verifier("6E5879A40CBEB7CCF77507BE8FE905AA"); 
     Token scribeAccessToken = service.getAccessToken(scribeRequestToken, scribeVerifier); 
     EvernoteAuth evernoteAuth = EvernoteAuth.parseOAuthResponse(EvernoteService.SANDBOX, scribeAccessToken.getRawResponse()); 
     String accessToken = evernoteAuth.getToken(); 
     String noteStoreUrl = evernoteAuth.getNoteStoreUrl(); 
     System.out.println("Access token :: " + accessToken); 

     evernoteAuth = new EvernoteAuth(EvernoteService.SANDBOX, accessToken); 
     NoteStoreClient noteStoreClient = new ClientFactory(evernoteAuth).createNoteStoreClient(); 

     List<Notebook> notebooks = noteStoreClient.listNotebooks(); 
     for (Notebook notebook : notebooks) { 
      System.out.println("Notebook: " + notebook.getName()); 
     } 
    } 
} 

此代碼與下面的GitHub代碼寫成參考https://github.com/evernote/evernote-sdk-java/blob/master/sample/oauth/src/main/webapp/index.jsp

下面是我得到在檢索訪問令牌中的例外

Exception in thread "main" org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this 

at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:41) 
    at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:27) 
    at org.scribe.oauth.OAuth10aServiceImpl.getAccessToken(OAuth10aServiceImpl.java:82) 
    at com.evernote.auth.EverNoteOauthGenerator.main(EverNoteOauthGenerator.java:51) 

回答

2

的回調網址必須是您的應用獲得授權後用戶重定向到的URL。當用戶重定向回來時,您應該能夠從URL參數中獲得OAuth驗證程序,而驗證程序似乎是在您的代碼中進行硬編碼的。有關驗證,另請參閱this doc

+0

我想作爲一個java propgram第一。在這種情況下,我配置了什麼回調URL? –

+0

本地主機怎麼樣? https://dev.evernote.com/doc/articles/authentication.php#temporary_token_request – kentaro

相關問題