Skip to content

Commit 0cb3301

Browse files
add support for LinekdIn scopes
1 parent 1453dfb commit 0cb3301

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

src/main/java/org/scribe/builder/api/LinkedInApi.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
package org.scribe.builder.api;
22

33
import org.scribe.model.*;
4+
import java.util.*;
45

56
public class LinkedInApi extends DefaultApi10a
67
{
78
private static final String AUTHORIZE_URL = "https://api.linkedin.com/uas/oauth/authenticate?oauth_token=%s";
9+
private static final String REQUEST_TOKEN_URL = "https://api.linkedin.com/uas/oauth/requestToken";
10+
11+
private final Set<String> scopes;
12+
13+
public LinkedInApi()
14+
{
15+
scopes = Collections.EMPTY_SET;
16+
}
17+
18+
public LinkedInApi(Set<String> scopes)
19+
{
20+
this.scopes = Collections.unmodifiableSet(scopes);
21+
}
822

923
@Override
1024
public String getAccessTokenEndpoint()
@@ -15,13 +29,29 @@ public String getAccessTokenEndpoint()
1529
@Override
1630
public String getRequestTokenEndpoint()
1731
{
18-
return "https://api.linkedin.com/uas/oauth/requestToken";
32+
return scopes.isEmpty() ? REQUEST_TOKEN_URL : REQUEST_TOKEN_URL + "?scopes=" + scopesAsString();
1933
}
20-
34+
35+
private String scopesAsString()
36+
{
37+
StringBuilder builder = new StringBuilder();
38+
for(String scope : scopes)
39+
{
40+
builder.append("+" + scope);
41+
}
42+
return builder.substring(1);
43+
}
44+
2145
@Override
2246
public String getAuthorizationUrl(Token requestToken)
2347
{
2448
return String.format(AUTHORIZE_URL, requestToken.getToken());
2549
}
50+
51+
public static LinkedInApi withScopes(String... scopes)
52+
{
53+
Set<String> scopeSet = new HashSet<String>(Arrays.asList(scopes));
54+
return new LinkedInApi(scopeSet);
55+
}
2656

2757
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.scribe.examples;
2+
3+
import java.util.Scanner;
4+
5+
import org.scribe.builder.*;
6+
import org.scribe.builder.api.*;
7+
import org.scribe.model.*;
8+
import org.scribe.oauth.*;
9+
10+
public class LinkedInExampleWithScopes
11+
{
12+
private static final String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~/connections:(id,last-name)";
13+
14+
public static void main(String[] args)
15+
{
16+
OAuthService service = new ServiceBuilder()
17+
.provider(LinkedInApi.withScopes("foo", "bar", "baz"))
18+
.apiKey("CiEgwWDkA5BFpNrc0RfGyVuSlOh4tig5kOTZ9q97qcXNrFl7zqk-Ts7DqRGaKDCV")
19+
.apiSecret("dhho4dfoCmiQXrkw4yslork5XWLFnPSuMR-8gscPVjY4jqFFHPYWJKgpFl4uLTM6")
20+
.build();
21+
Scanner in = new Scanner(System.in);
22+
23+
System.out.println("=== LinkedIn's OAuth Workflow ===");
24+
System.out.println();
25+
26+
// Obtain the Request Token
27+
System.out.println("Fetching the Request Token...");
28+
Token requestToken = service.getRequestToken();
29+
System.out.println("Got the Request Token!");
30+
System.out.println();
31+
32+
System.out.println("Now go and authorize Scribe here:");
33+
System.out.println(service.getAuthorizationUrl(requestToken));
34+
System.out.println("And paste the verifier here");
35+
System.out.print(">>");
36+
Verifier verifier = new Verifier(in.nextLine());
37+
System.out.println();
38+
39+
// Trade the Request Token and Verfier for the Access Token
40+
System.out.println("Trading the Request Token for an Access Token...");
41+
Token accessToken = service.getAccessToken(requestToken, verifier);
42+
System.out.println("Got the Access Token!");
43+
System.out.println("(if your curious it looks like this: " + accessToken + " )");
44+
System.out.println();
45+
46+
// Now let's go and ask for a protected resource!
47+
System.out.println("Now we're going to access a protected resource...");
48+
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
49+
service.signRequest(accessToken, request);
50+
Response response = request.send();
51+
System.out.println("Got it! Lets see what we found...");
52+
System.out.println();
53+
System.out.println(response.getBody());
54+
55+
System.out.println();
56+
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
57+
}
58+
59+
}

0 commit comments

Comments
 (0)