我正在尝试使用Java SignPost从discogs获得Oauth支持
如果我调用这个方法,它提供一个url到一个网站,然后如果我登录,我得到一个验证码,到目前为止一切顺利。
public static String requestDiscogsAuthorization() throws Exception
{
OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
OAuthProvider provider = new DefaultOAuthProvider(
"http://api.discogs.com/oauth/request_token",
"http://api.discogs.com/oauth/access_token",
"http://www.discogs.com/oauth/authorize");
provider.setRequestHeader("User-Agent", SongKong.USER_AGENT);
return provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
}
我将此验证码存储在我的应用程序中,并可以作为UserPreferences.getInstance(). getdiscogsauthorization()进行检索。现在我想对url签名我尝试调用下面的getAccessToken()从验证密钥中获取访问码但得到的是
oauth.signpost.exception.OAuthExpectationFailedException: Authorized request token or token secret not set. Did you retrieve an authorized request token before?
at oauth.signpost.AbstractOAuthProvider.retrieveAccessToken(AbstractOAuthProvider.java:89)
at com.jthink.songkong.analyse.toplevelanalyzer.DiscogsAuth.getAccessToken(DiscogsAuth.java:54)
我不知道我做错了什么
public class DiscogsAuth
{
/**
* Constructs a consumer with valid access token for signing urls (can only be used once user has authorizaed application and auth code
* available to application somewhere
*
* NOTE:Not thread safe, has to be done once for each thread
*
* @return
* @throws Exception
*/
public static OAuthConsumer getAccessToken() throws Exception
{
OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
OAuthProvider provider = new DefaultOAuthProvider(
"http://api.discogs.com/oauth/request_token",
"http://api.discogs.com/oauth/access_token",
"http://www.discogs.com/oauth/authorize");
provider.setRequestHeader("User-Agent", SongKong.USER_AGENT);
provider.retrieveAccessToken(consumer, UserPreferences.getInstance().getDiscogsAuthorization());
return consumer;
}
/**
* Sign Url Connection
*
* @param urlConnection
* @param consumer
* @throws Exception
*/
public static void signUrl(HttpURLConnection urlConnection, OAuthConsumer consumer) throws Exception
{
consumer.sign(urlConnection);
}
}
当您尝试调用提供程序时,可能您的方法调用无法获得验证码。retrieveAccessToken,您可以尝试在将验证码传递给提供者之前打印验证码。retrieveAccessToken方法。
您缺少访问令牌和访问令牌secret。
要签署OAuth 1.0请求,您需要三样东西:您已经拥有的CONSUMER_SECRET
, 访问令牌和访问令牌secret,您将在临时凭据请求的响应中分别在oauth_token
和oauth_token_secret
变量中获得。
我没有Java经验,但是根据Signpost文档,我猜你应该把这个叫做…
consumer.setTokenWithSecret(ACCESS_TOKEN, TOKEN_SECRET);
…在尝试对url
进行签名之前consumer.sign(urlConnection);
似乎你可以得到ACCESS_TOKEN
和TOKEN_SECRET
与…
String accessToken = provider.getResponseParameter('oauth_token');
String tokenSecret = provider.getResponseParameter('oauth_token_secret');
…在你调用这个之后:
provider.retrieveAccessToken(consumer, verificationCode);
这是我的看法(我没有尝试过):
public class DiscogsAuth
{
/**
* Constructs a consumer with valid access token for signing urls (can only be used once user has authorizaed application and auth code
* available to application somewhere
*
* NOTE:Not thread safe, has to be done once for each thread
*
* @return
* @throws Exception
*/
public static OAuthConsumer getAccessToken() throws Exception
{
OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
OAuthProvider provider = new DefaultOAuthProvider(
"http://api.discogs.com/oauth/request_token",
"http://api.discogs.com/oauth/access_token",
"http://www.discogs.com/oauth/authorize");
provider.setRequestHeader("User-Agent", SongKong.USER_AGENT);
provider.retrieveAccessToken(consumer, UserPreferences.getInstance().getDiscogsAuthorization());
// Retrieve access token and access token secret
String accessToken = provider.getResponseParameter('oauth_token');
String tokenSecret = provider.getResponseParameter('oauth_token_secret');
// Set them on the consumer so it can sign the requestst with them
consumer.setTokenWithSecret(accessToken, tokenSecret);
return consumer;
}
/**
* Sign Url Connection
*
* @param urlConnection
* @param consumer
* @throws Exception
*/
public static void signUrl(HttpURLConnection urlConnection, OAuthConsumer consumer) throws Exception
{
consumer.sign(urlConnection);
}
}
问题是我没有为每一步使用相同的OAuthConsumer和OAuthProvider实例。