使用 scribe 检索 OAuth 访问令牌时出现异常
Exception while retrieving OAuth access token using scribe
我正在尝试为我的应用程序生成 OAuth 访问令牌以从 Evernote 获取笔记。下面是它的 Java 代码
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)
回调 URL 必须是您的 URL 在您的应用获得授权后用户被重定向到的地方。当用户被重定向回来时,您应该能够从 URL 参数中获得 OAuth 验证器,而验证器似乎是硬编码在您的代码中的。另请参阅 this doc 关于身份验证。
我正在尝试为我的应用程序生成 OAuth 访问令牌以从 Evernote 获取笔记。下面是它的 Java 代码
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)
回调 URL 必须是您的 URL 在您的应用获得授权后用户被重定向到的地方。当用户被重定向回来时,您应该能够从 URL 参数中获得 OAuth 验证器,而验证器似乎是硬编码在您的代码中的。另请参阅 this doc 关于身份验证。