Skip to content

Commit 4f84b31

Browse files
committed
Update LinkedIn Example to API v2 (thanks to https://github.com/peternees)
1 parent 030d768 commit 4f84b31

2 files changed

Lines changed: 29 additions & 23 deletions

File tree

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[SNAPSHOT]
22
* add PMD checks on compile
33
* add all OAuth error codes from supported RFCs (incl. "invalid_token") (thanks to https://github.com/echorebel)
4+
* Update LinkedIn Example to API v2 (thanks to https://github.com/peternees)
45

56
[6.5.1]
67
* cleanup deprecates methods

scribejava-apis/src/test/java/com/github/scribejava/apis/examples/LinkedIn20Example.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
import com.github.scribejava.core.model.Verb;
1010
import com.github.scribejava.core.oauth.OAuth20Service;
1111
import java.io.IOException;
12+
import java.util.Random;
1213
import java.util.concurrent.ExecutionException;
1314

1415
public class LinkedIn20Example {
1516

1617
private static final String NETWORK_NAME = "LinkedIn";
17-
private static final String PROTECTED_RESOURCE_URL = "https://api.linkedin.com/v1/people/~:(%s)";
18+
private static final String PROTECTED_RESOURCE_URL = "https://api.linkedin.com/v2/me";
19+
private static final String PROTECTED_EMAIL_RESOURCE_URL
20+
= "https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))";
1821

1922
private LinkedIn20Example() {
2023
}
@@ -26,7 +29,7 @@ public static void main(String... args) throws IOException, InterruptedException
2629
final String clientSecret = "your client secret";
2730
final OAuth20Service service = new ServiceBuilder(clientId)
2831
.apiSecret(clientSecret)
29-
.defaultScope("r_basicprofile r_emailaddress") // replace with desired scope
32+
.defaultScope("r_liteprofile r_emailaddress") // replace with desired scope
3033
.callback("http://example.com/callback")
3134
.build(LinkedInApi20.instance());
3235
final Scanner in = new Scanner(System.in);
@@ -36,7 +39,8 @@ public static void main(String... args) throws IOException, InterruptedException
3639

3740
// Obtain the Authorization URL
3841
System.out.println("Fetching the Authorization URL...");
39-
final String authorizationUrl = service.getAuthorizationUrl("some_params");
42+
final String secretState = "secret" + new Random().nextInt(999_999);
43+
final String authorizationUrl = service.getAuthorizationUrl(secretState);
4044
System.out.println("Got the Authorization URL!");
4145
System.out.println("Now go and authorize ScribeJava here:");
4246
System.out.println(authorizationUrl);
@@ -52,28 +56,29 @@ public static void main(String... args) throws IOException, InterruptedException
5256
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
5357
System.out.println();
5458

55-
// Now let's go and ask for a protected resource!
56-
System.out.println("Now we're going to access a protected resource...");
57-
while (true) {
58-
System.out.println("Paste profile query for fetch (firstName, lastName, etc) or 'exit' to stop example");
59-
System.out.print(">>");
60-
final String query = in.nextLine();
61-
System.out.println();
59+
System.out.println("Now we're going to get the email of the current user...");
60+
final OAuthRequest emailRequest = new OAuthRequest(Verb.GET, PROTECTED_EMAIL_RESOURCE_URL);
61+
emailRequest.addHeader("x-li-format", "json");
62+
emailRequest.addHeader("Accept-Language", "ru-RU");
63+
service.signRequest(accessToken, emailRequest);
64+
final Response emailResponse = service.execute(emailRequest);
65+
System.out.println();
66+
System.out.println(emailResponse.getCode());
67+
System.out.println(emailResponse.getBody());
6268

63-
if ("exit".equals(query)) {
64-
break;
65-
}
69+
System.out.println();
6670

67-
final OAuthRequest request = new OAuthRequest(Verb.GET, String.format(PROTECTED_RESOURCE_URL, query));
68-
request.addHeader("x-li-format", "json");
69-
request.addHeader("Accept-Language", "ru-RU");
70-
service.signRequest(accessToken, request);
71-
final Response response = service.execute(request);
72-
System.out.println();
73-
System.out.println(response.getCode());
74-
System.out.println(response.getBody());
71+
System.out.println("Now we're going to access a protected profile resource...");
7572

76-
System.out.println();
77-
}
73+
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
74+
request.addHeader("x-li-format", "json");
75+
request.addHeader("Accept-Language", "ru-RU");
76+
service.signRequest(accessToken, request);
77+
final Response response = service.execute(request);
78+
System.out.println();
79+
System.out.println(response.getCode());
80+
System.out.println(response.getBody());
81+
82+
System.out.println();
7883
}
7984
}

0 commit comments

Comments
 (0)