适用于 Android 的 OAuth 1.0 库 (java)
我正在尝试从支持 OAuth 1.0 的站点访问用户数据,而无需回调。我正在使用路标库,但无法检索访问令牌,因为我尝试连接的网站不需要用户指定验证码。要从路标获取访问令牌,我似乎需要提供某种类型的验证码。
我的问题:有没有办法绕过此步骤并仍然获得访问令牌?
否则,我会收到消息“授权失败(服务器回复 401)”。我已经设置/注册了我的客户。
我已经包含了我现有的代码以及有关我尝试连接的网站的更多详细信息(http://www.stepgreen.org - 您必须创建一个帐户才能查看以下信息):
身份验证:OAuth
StepGreens 首选的身份验证方法是 OAuth。有关 OAuth 的更多信息,您应该访问 OAuth.net。我们目前支持不带回调的 OAuth 1.0。
您将需要一些附加信息来配置您选择的 OAuth 库:
请求请求令牌:/oauth/request_token 请求访问令牌:/oauth/access_token 授权网址:/oauth/authorize 要对您的客户端使用 OAuth,请记住设置您的客户端。
public static void main(String[] args) throws Exception{
OAuthConsumer consumer = new DefaultOAuthConsumer( CONSUMER_KEY, CONSUMER_SECRET,
SignatureMethod.HMAC_SHA1);
OAuthProvider provider = new DefaultOAuthProvider(consumer, REQUEST_TOKEN_URL,
ACCESS_TOKEN_URL, AUTHORIZE_URL);
System.out.println("Fetching request token...");
String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND);
System.out.println("Request token: " + consumer.getToken());
System.out.println("Token secret: " + consumer.getTokenSecret());
System.out.println("Now visit:\n" + authUrl + "\n... and grant this app authorization");
System.out.println("Enter the verification code and hit ENTER when you're done:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String verificationCode = br.readLine();
System.out.println("Fetching access token...");
String verificationCode = consumer.getToken();
provider.retrieveAccessToken(verificationCode.trim());
System.out.println("Access token: " + consumer.getToken());
System.out.println("Token secret: " + consumer.getTokenSecret());
URL url = new URL("http://www.stepgreen.org/api/v1/users/username/ted5000s.xml");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
consumer.sign(request);
InputStream stream = (InputStream)request.getContent();
System.out.println("Result = " + convertStreamToString(stream));
System.out.println("Sending request...");
request.connect();
System.out.println("Response code: " + request.getResponseCode() + " " +
request.getResponseMessage());
响应
:
正在获取请求令牌...
请求令牌:出于隐私原因隐藏
令牌秘密:出于隐私原因隐藏
现在访问: http://www.stepgreen.org/oauth/authorize?oauth_token=hidden ...并授予此应用程序授权 输入验证码,完成后按 ENTER:
正在获取访问令牌... 线程“main”中出现异常 oauth.signpost.exception.OAuthNotAuthorizedException:授权失败(服务器回复 401)。如果消费者密钥不正确或签名不匹配,则可能会发生这种情况。 在 oauth.signpost.basic.DefaultOAuthProvider.retrieveToken(DefaultOAuthProvider.java:126) 在 oauth.signpost.basic.DefaultOAuthProvider.retrieveAccessToken(DefaultOAuthProvider.java:96) 在 Test.main(Test.java:89)
I'm trying to access user data from a site that supports OAuth 1.0 without callbacks. I'm using the signpost library and I'm not able to retrieve the access token because the site I'm trying to connect to doesn't require the user to specify a verification code. To get the access token from signpost, it seems I need to provide some type of verification code.
MY QUESTION: Is there a way to bypass this step and still obtain an access token?
Otherwise, I get the message "Authorization failed (server replied with a 401)". I've set up/registered my client.
I've included my existing code and more detail about the site I'm trying to connect to (http://www.stepgreen.org - you'd have to create an account to see the information below):
Authentication: OAuth
StepGreens preferred method of authentication is OAuth. For more information on OAuth you should visit OAuth.net. We currently support OAuth 1.0 without callbacks.
You will need some additional information to configure your OAuth library of choice:
request a Request Token: /oauth/request_token
request an Access Token: /oauth/access_token
Authorize URL: /oauth/authorize
To use OAuth with your clients, remember to setup your Clients.
public static void main(String[] args) throws Exception{
OAuthConsumer consumer = new DefaultOAuthConsumer( CONSUMER_KEY, CONSUMER_SECRET,
SignatureMethod.HMAC_SHA1);
OAuthProvider provider = new DefaultOAuthProvider(consumer, REQUEST_TOKEN_URL,
ACCESS_TOKEN_URL, AUTHORIZE_URL);
System.out.println("Fetching request token...");
String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND);
System.out.println("Request token: " + consumer.getToken());
System.out.println("Token secret: " + consumer.getTokenSecret());
System.out.println("Now visit:\n" + authUrl + "\n... and grant this app authorization");
System.out.println("Enter the verification code and hit ENTER when you're done:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String verificationCode = br.readLine();
System.out.println("Fetching access token...");
String verificationCode = consumer.getToken();
provider.retrieveAccessToken(verificationCode.trim());
System.out.println("Access token: " + consumer.getToken());
System.out.println("Token secret: " + consumer.getTokenSecret());
URL url = new URL("http://www.stepgreen.org/api/v1/users/username/ted5000s.xml");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
consumer.sign(request);
InputStream stream = (InputStream)request.getContent();
System.out.println("Result = " + convertStreamToString(stream));
System.out.println("Sending request...");
request.connect();
System.out.println("Response code: " + request.getResponseCode() + " " +
request.getResponseMessage());
}
Response:
Fetching request token...
Request token: hidden for privacy reasons
Token secret: hidden for privacy reasons
Now visit:
http://www.stepgreen.org/oauth/authorize?oauth_token=hidden
... and grant this app authorization
Enter the verification code and hit ENTER when you're done:
Fetching access token...
Exception in thread "main" oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.
at oauth.signpost.basic.DefaultOAuthProvider.retrieveToken(DefaultOAuthProvider.java:126)
at oauth.signpost.basic.DefaultOAuthProvider.retrieveAccessToken(DefaultOAuthProvider.java:96)
at Test.main(Test.java:89)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论