According to an error message I was getting when I tried to get an authentication request token through service.getRequestToken()
, I am not actually allowed to do it that way, and must instead use service.getAuthorizationUrl()
to redirect the user. The problem is, i don't know what to do after that. I don't have a request token to give the service.getAccessToken()
, and I have no idea how to get the appropriate verifier, either.
- How do I get the needed verifier that I need in order to get an access token?
- How do I get the needed request token, since I can't use
service.getRequestToken()
?
Here is the code I have so far:
import java.awt.Desktop;
import java.net.URI;
import org.scribe.model.*;
import org.scribe.oauth.OAuthService;
public class HelloBox{
public static void main(String[] args) throws Exception{
BoxOAuth2API box = new BoxOAuth2API();
OAuthConfig config = new OAuthConfig(
/* [...] */, //client id
/* [...] */, //client secret
"https://localhost:4000", //callback
null, null, //signature type, scope
System.out //debug stream
);
OAuthService service = box.createService(config);
Desktop.getDesktop().browse(URI.create(service.getAuthorizationUrl(null)));
// === What do I do now? ===
Verifier v = new Verifier( /* ??? */ );
Token accessToken = service.getAccessToken(null, v);
}
}
I found this BoxOAuth2API
class online. If the problem is with this, where is an API class that does work?
public class BoxOAuth2API extends DefaultApi20 {
//http://developers.box.com/docs/
private static final String AUTHORIZE_URL = "https://www.box.com/api/oauth2/authorize?client_id=%s&redirect_uri=%s&response_type=code";
private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=%s";
@Override
public Verb getAccessTokenVerb() {
return Verb.POST;
}
@Override
public AccessTokenExtractor getAccessTokenExtractor() {
return new JsonTokenExtractor();
}
@Override
public String getAccessTokenEndpoint() {
return "https://www.box.com/api/oauth2/token?grant_type=authorization_code";
}
@Override
public String getAuthorizationUrl(OAuthConfig config) {
// Append scope if present
if (config.hasScope()) {
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(),
OAuthEncoder.encode(config.getCallback()),
OAuthEncoder.encode(config.getScope()));
} else {
return String.format(AUTHORIZE_URL, config.getApiKey(),
OAuthEncoder.encode(config.getCallback()));
}
}
}