Advertisement
Guest User

Untitled

a guest
Dec 26th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. === Box's OAuth Workflow ===
  2.  
  3. Fetching the Authorization URL...
  4. Got the Authorization URL!
  5. Now go and authorize Scribe here:
  6. https://www.box.com/api/oauth2/authorize?client_id=abc123&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fjenkins%2Fconfigure&response_type=code&state=authenticated
  7. And paste the authorization code here
  8. >>xyz9876543
  9.  
  10. Trading the Request Token for an Access Token...
  11. Exception in thread "main" org.scribe.exceptions.OAuthException: Cannot extract an acces token. Response was: {"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}
  12. at org.scribe.extractors.JsonTokenExtractor.extract(JsonTokenExtractor.java:23)
  13. at org.scribe.oauth.OAuth20ServiceImpl.getAccessToken(OAuth20ServiceImpl.java:37)
  14. at com.example.box.oauth2.Box2.main(Box2.java:40)
  15.  
  16. public class Box2 {
  17. private static final Token EMPTY_TOKEN = null;
  18.  
  19. public static void main(String[] args) {
  20. // Replace these with your own api key and secret
  21. String apiKey = "abc123";
  22. String apiSecret = "xyz987";
  23. OAuthService service = new ServiceBuilder().provider(BoxApi.class)
  24. .apiKey(apiKey).apiSecret(apiSecret)
  25. .callback("http://localhost:8080/jenkins/configure")
  26. .build();
  27. Scanner in = new Scanner(System.in);
  28.  
  29. System.out.println("=== Box's OAuth Workflow ===");
  30. System.out.println();
  31.  
  32. // Obtain the Authorization URL
  33. System.out.println("Fetching the Authorization URL...");
  34. String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
  35. System.out.println("Got the Authorization URL!");
  36. System.out.println("Now go and authorize Scribe here:");
  37. System.out.println(authorizationUrl);
  38. System.out.println("And paste the authorization code here");
  39. System.out.print(">>");
  40. Verifier verifier = new Verifier(in.nextLine());
  41. System.out.println();
  42.  
  43. // Trade the Request Token and Verfier for the Access Token
  44. System.out.println("Trading the Request Token for an Access Token...");
  45. Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
  46. System.out.println("Got the Access Token!");
  47. System.out.println("(if your curious it looks like this: "
  48. + accessToken + " )");
  49. System.out.println();
  50.  
  51.  
  52. }
  53. }
  54.  
  55. public class BoxApi extends DefaultApi20 {
  56. private static final String AUTHORIZATION_URL =
  57. "https://www.box.com/api/oauth2/authorize?client_id=%s&redirect_uri=%s&response_type=code&state=authenticated";
  58.  
  59. @Override
  60. public String getAccessTokenEndpoint() {
  61. return "https://www.box.com/api/oauth2/token?grant_type=authorization_code";
  62. }
  63.  
  64.  
  65. @Override
  66. public String getAuthorizationUrl(OAuthConfig config) {
  67. return String.format(AUTHORIZATION_URL, config.getApiKey(),
  68. OAuthEncoder.encode(config.getCallback()));
  69. }
  70.  
  71. @Override
  72. public Verb getAccessTokenVerb(){
  73. return Verb.POST;
  74. }
  75.  
  76. @Override
  77. public AccessTokenExtractor getAccessTokenExtractor() {
  78. return new JsonTokenExtractor();
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement