要件:クラウド アプリケーションに存在するリソースにアクセスしたい。
このクラウド アプリケーションは、 OAuth 1.0認証を介してリソースにアクセスするための次の詳細を提供してくれました。
OAuth 資格情報
- コンシューマ キー
- 消費者の秘密
OAuth リクエスト URL
1. Request Token URL
2. Authorise URL
3. Access Token URL
4. API Endpoint URL
Request TokenとRequest Token Secretを取得するために、次の Java コードを作成しました。Access 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を使用して、サービス プロバイダーからリクエスト トークンとリクエスト トークン シークレットを取得できます。
次に、生成された Request Token と Request Token Secrete を使用して、次のコードを使用して Access Token を取得しました
OAuthAccessor accessor = createOAuthAccessor();
accessor.tokenSecret = props.getProperty("tokenSecret");
OAuthClient client = new OAuthClient(new HttpClient4());
return client.invoke(accessor, "GET", url, params);
アクセストークンを取得するために上記のコードを実行した後、次の例外が発生しました
上記のコードでclient.invoke() に URL パラメーターの値としてAPI エンドポイント URL /Resourceを渡すと、次の例外が発生します。
> <<<<<<<< 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.
質問:
- Access Token を取得するには、どの URL を使用すればよいですか?
- アクセス トークンを取得するための手順や設定が不足していませんか?
前もって感謝します。