Browse files

add support for LinekdIn scopes

  • Loading branch information...
1 parent 1453dfb commit 0cb3301b8e07a54c7ff8bdd99961de421e6a49f2 @fernandezpablo85 fernandezpablo85 committed Feb 12, 2013
View
34 src/main/java/org/scribe/builder/api/LinkedInApi.java
@@ -1,10 +1,24 @@
package org.scribe.builder.api;
import org.scribe.model.*;
+import java.util.*;
public class LinkedInApi extends DefaultApi10a
{
private static final String AUTHORIZE_URL = "https://api.linkedin.com/uas/oauth/authenticate?oauth_token=%s";
+ private static final String REQUEST_TOKEN_URL = "https://api.linkedin.com/uas/oauth/requestToken";
+
+ private final Set<String> scopes;
+
+ public LinkedInApi()
+ {
+ scopes = Collections.EMPTY_SET;
+ }
+
+ public LinkedInApi(Set<String> scopes)
+ {
+ this.scopes = Collections.unmodifiableSet(scopes);
+ }
@Override
public String getAccessTokenEndpoint()
@@ -15,13 +29,29 @@ public String getAccessTokenEndpoint()
@Override
public String getRequestTokenEndpoint()
{
- return "https://api.linkedin.com/uas/oauth/requestToken";
+ return scopes.isEmpty() ? REQUEST_TOKEN_URL : REQUEST_TOKEN_URL + "?scopes=" + scopesAsString();
}
-
+
+ private String scopesAsString()
+ {
+ StringBuilder builder = new StringBuilder();
+ for(String scope : scopes)
+ {
+ builder.append("+" + scope);
+ }
+ return builder.substring(1);
+ }
+
@Override
public String getAuthorizationUrl(Token requestToken)
{
return String.format(AUTHORIZE_URL, requestToken.getToken());
}
+
+ public static LinkedInApi withScopes(String... scopes)
+ {
+ Set<String> scopeSet = new HashSet<String>(Arrays.asList(scopes));
+ return new LinkedInApi(scopeSet);
+ }
}
View
59 src/test/java/org/scribe/examples/LinkedInExampleWithScopes.java
@@ -0,0 +1,59 @@
+package org.scribe.examples;
+
+import java.util.Scanner;
+
+import org.scribe.builder.*;
+import org.scribe.builder.api.*;
+import org.scribe.model.*;
+import org.scribe.oauth.*;
+
+public class LinkedInExampleWithScopes
+{
+ private static final String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~/connections:(id,last-name)";
+
+ public static void main(String[] args)
+ {
+ OAuthService service = new ServiceBuilder()
+ .provider(LinkedInApi.withScopes("foo", "bar", "baz"))
+ .apiKey("CiEgwWDkA5BFpNrc0RfGyVuSlOh4tig5kOTZ9q97qcXNrFl7zqk-Ts7DqRGaKDCV")
+ .apiSecret("dhho4dfoCmiQXrkw4yslork5XWLFnPSuMR-8gscPVjY4jqFFHPYWJKgpFl4uLTM6")
+ .build();
+ Scanner in = new Scanner(System.in);
+
+ System.out.println("=== LinkedIn's OAuth Workflow ===");
+ System.out.println();
+
+ // Obtain the Request Token
+ System.out.println("Fetching the Request Token...");
+ Token requestToken = service.getRequestToken();
+ System.out.println("Got the Request Token!");
+ System.out.println();
+
+ System.out.println("Now go and authorize Scribe here:");
+ System.out.println(service.getAuthorizationUrl(requestToken));
+ System.out.println("And paste the verifier here");
+ System.out.print(">>");
+ Verifier verifier = new Verifier(in.nextLine());
+ System.out.println();
+
+ // Trade the Request Token and Verfier for the Access Token
+ System.out.println("Trading the Request Token for an Access Token...");
+ Token accessToken = service.getAccessToken(requestToken, verifier);
+ System.out.println("Got the Access Token!");
+ System.out.println("(if your curious it looks like this: " + accessToken + " )");
+ System.out.println();
+
+ // Now let's go and ask for a protected resource!
+ System.out.println("Now we're going to access a protected resource...");
+ OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
+ service.signRequest(accessToken, request);
+ Response response = request.send();
+ System.out.println("Got it! Lets see what we found...");
+ System.out.println();
+ System.out.println(response.getBody());
+
+ System.out.println();
+ System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
+ }
+
+}

2 comments on commit 0cb3301

@developez

Too much complex, because if provider method of ServiceBuilder can receive a instance and not only a Class object that static method is redundant and it could limite future modifications. I did know that you can pass a instance to provider.

in other hand the url must contain "scope" and not "scopes".
Also the naming is incorrect because are scope parameters and not scopes.

anyway the changes are welcome, than you very much.

@fernandezpablo85

fixed the typo. Thank you.

Please sign in to comment.