要求:我想访问驻留在云应用程序中的资源。
这个云应用程序为我提供了以下详细信息,以通过OAuth 1.0身份验证访问资源。
OAuth 凭据
- 消费者密钥
- 消费者秘密
OAuth 请求 URL
1. Request Token URL
2. Authorise URL
3. Access Token URL
4. API Endpoint URL
我编写了以下 java 代码来获取Request Token和Request Token Secret。我将请求令牌和秘密存储在属性文件中以检索访问令牌。
OAuthAccessor accessor = createOAuthAccessor();
OAuthClient client = new OAuthClient(new HttpClient4());
client.getRequestToken(accessor);
props.setProperty("requestToken", accessor.requestToken);
props.setProperty("tokenSecret", accessor.tokenSecret);
private OAuthAccessor createOAuthAccessor(){
String consumerKey = props.getProperty("consumerKey");
String callbackUrl = null;
String consumerSecret = props.getProperty("consumerSecret");
String reqUrl = props.getProperty("requestUrl");
String authzUrl = props.getProperty("authorizationUrl");
String accessUrl = props.getProperty("accessUrl");
OAuthServiceProvider provider
= new OAuthServiceProvider(reqUrl, authzUrl, accessUrl);
OAuthConsumer consumer
= new OAuthConsumer(callbackUrl, consumerKey,
consumerSecret, provider);
return new OAuthAccessor(consumer);
}
属性文件详细信息:
requestToken= generated by service provider
authorizationUrl= Authorise URL provided by cloud application
consumerSecret= Consumer Secret provided by cloud application
accessUrl=Access Token URL provided by cloud application
tokenSecret= generated by service provider
requestUrl= Request Token URL provided by cloud application
consumerKey= Consumer Secret provided by cloud application
appName= API Endpoint URL provided by cloud application
我可以使用云应用程序提供的请求令牌 URL从服务提供商处检索请求令牌和请求令牌秘密。
然后我使用生成的请求令牌和请求令牌秘密通过以下代码获取访问令牌
OAuthAccessor accessor = createOAuthAccessor();
accessor.tokenSecret = props.getProperty("tokenSecret");
OAuthClient client = new OAuthClient(new HttpClient4());
return client.invoke(accessor, "GET", url, params);
在执行上述代码以检索访问令牌后,我得到了以下异常
如果我在上面的代码中将API Endpoint URL /Resource 作为 URL 参数的值传递给client.invoke()那么我得到以下异常
> <<<<<<<< HTTP response: HTTP/1.1 401 Unauthorized Cache-Control:
> private Content-Type: text/html; charset=utf-8 WWW-Authenticate: OAuth
> Realm="115.248.52.162" X-S: 445759-O1VMAP02 Strict-Transport-Security:
> max-age=31536000 Date: Tue, 18 Jun 2013 06:59:28 GMT Content-Length:
> 142
>
> Exception in thread "main" net.oauth.OAuthProblemException:
> token_rejected oauth_problem_advice: Token RZXHZYCCUMNMZA88032WJFB
> does not match an expected ACCESS token
如果我在client.invoke() 中将访问令牌 URL作为 URL 参数的值传递,那么我将收到以下异常
> <<<<<<<< HTTP response: HTTP/1.1 401 Unauthorized Cache-Control:
> private Content-Type: text/html; charset=utf-8 WWW-Authenticate: OAuth
> Realm="49.248.38.202" X-S: 445758-O1VMAP01 Strict-Transport-Security:
> max-age=31536000 Date: Tue, 18 Jun 2013 05:47:30 GMT Content-Length:
> 115
>
> oauth_problem=permission_denied&oauth_problem_advice=The%20consumer%20was%20denied%20access%20to%20this%20resource.
问题:
- 我应该使用哪个 URL 来获取访问令牌?
- 我是否缺少检索访问令牌的任何步骤或设置?
提前致谢。