Skip to content

Commit 875db91

Browse files
committed
1 parent 4e6d8c5 commit 875db91

File tree

6 files changed

+102
-116
lines changed

6 files changed

+102
-116
lines changed

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* fix missing support for scope for refresh_token grant_type (thanks to https://github.com/tlxtellef)
33
* add email field to VKOAuth2AccessToken (thanks to https://github.com/grouzen)
44
* add new API - Automatic (https://www.automatic.com/) (thanks to https://github.com/ramsrib)
5+
* add new API - Fitbit (https://www.fitbit.com/) (thanks to https://github.com/JustinLawler and https://github.com/alexthered)
56

67
[5.3.0]
78
* fix Salesforce API (thanks to https://github.com/jhorowitz-firedrum)

scribejava-apis/src/main/java/com/github/scribejava/apis/FitbitApi20.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package com.github.scribejava.apis;
22

3-
import com.github.scribejava.apis.service.Fitbit20ServiceImpl;
3+
import com.github.scribejava.apis.fitbit.FitBitJsonTokenExtractor;
44
import com.github.scribejava.core.builder.api.DefaultApi20;
5-
import com.github.scribejava.core.model.OAuthConfig;
6-
import com.github.scribejava.core.oauth.OAuth20Service;
75

86

97
/**
108
* Fitbit's OAuth2 client's implementation
119
* source: https://dev.fitbit.com/docs/oauth2/
12-
*
13-
* Note - this is an updated version of this library for Scribe v5.3.0. Original code here:
14-
* - https://github.com/alexthered/fitbitAPI20-scribe-java
1510
*/
1611
public class FitbitApi20 extends DefaultApi20 {
1712

@@ -37,7 +32,7 @@ protected String getAuthorizationBaseUrl() {
3732
}
3833

3934
@Override
40-
public OAuth20Service createService(OAuthConfig config) {
41-
return new Fitbit20ServiceImpl(this, config);
35+
public FitBitJsonTokenExtractor getAccessTokenExtractor() {
36+
return FitBitJsonTokenExtractor.instance();
4237
}
43-
}
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.scribejava.apis.fitbit;
2+
3+
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
4+
import java.util.regex.Pattern;
5+
6+
public class FitBitJsonTokenExtractor extends OAuth2AccessTokenJsonExtractor {
7+
8+
private static final Pattern USER_ID_REGEX_PATTERN = Pattern.compile("\"user_id\"\\s*:\\s*\"(\\S*?)\"");
9+
10+
protected FitBitJsonTokenExtractor() {
11+
}
12+
13+
private static class InstanceHolder {
14+
15+
private static final FitBitJsonTokenExtractor INSTANCE = new FitBitJsonTokenExtractor();
16+
}
17+
18+
public static FitBitJsonTokenExtractor instance() {
19+
return InstanceHolder.INSTANCE;
20+
}
21+
22+
@Override
23+
protected FitBitOAuth2AccessToken createToken(String accessToken, String tokenType, Integer expiresIn,
24+
String refreshToken, String scope, String response) {
25+
return new FitBitOAuth2AccessToken(accessToken, tokenType, expiresIn, refreshToken, scope,
26+
extractParameter(response, USER_ID_REGEX_PATTERN, false), response);
27+
}
28+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.github.scribejava.apis.fitbit;
2+
3+
import com.github.scribejava.core.model.OAuth2AccessToken;
4+
import java.util.Objects;
5+
6+
public class FitBitOAuth2AccessToken extends OAuth2AccessToken {
7+
8+
private static final long serialVersionUID = -6374486860742407411L;
9+
10+
private final String userId;
11+
12+
public FitBitOAuth2AccessToken(String accessToken, String openIdToken, String rawResponse) {
13+
this(accessToken, null, null, null, null, openIdToken, rawResponse);
14+
}
15+
16+
public FitBitOAuth2AccessToken(String accessToken, String tokenType, Integer expiresIn, String refreshToken,
17+
String scope, String userId, String rawResponse) {
18+
super(accessToken, tokenType, expiresIn, refreshToken, scope, rawResponse);
19+
this.userId = userId;
20+
}
21+
22+
public String getUserId() {
23+
return userId;
24+
}
25+
26+
@Override
27+
public int hashCode() {
28+
int hash = super.hashCode();
29+
hash = 37 * hash + Objects.hashCode(userId);
30+
return hash;
31+
}
32+
33+
@Override
34+
public boolean equals(Object obj) {
35+
if (this == obj) {
36+
return true;
37+
}
38+
if (obj == null) {
39+
return false;
40+
}
41+
if (getClass() != obj.getClass()) {
42+
return false;
43+
}
44+
if (!super.equals(obj)) {
45+
return false;
46+
}
47+
48+
return Objects.equals(userId, ((FitBitOAuth2AccessToken) obj).getUserId());
49+
}
50+
}

scribejava-apis/src/main/java/com/github/scribejava/apis/service/Fitbit20ServiceImpl.java

Lines changed: 0 additions & 91 deletions
This file was deleted.

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
11
package com.github.scribejava.apis.examples;
2+
23
import java.util.Scanner;
34

45
import com.github.scribejava.apis.FitbitApi20;
6+
import com.github.scribejava.apis.fitbit.FitBitOAuth2AccessToken;
57
import com.github.scribejava.core.builder.ServiceBuilder;
68
import com.github.scribejava.core.model.OAuth2AccessToken;
79
import com.github.scribejava.core.model.OAuthRequest;
810
import com.github.scribejava.core.model.Response;
911
import com.github.scribejava.core.model.Verb;
1012
import com.github.scribejava.core.oauth.OAuth20Service;
1113

12-
/**
13-
* Created by hd on 10/09/16.
14-
*/
1514
public class FitbitApi20Example {
1615

1716
private static final String NETWORK_NAME = "Fitbit";
18-
19-
// Replace with user ID to test API against
20-
private static final String USER_ID = "your user id";
21-
private static final String PROTECTED_RESOURCE_URL = "https://api.fitbit.com/1/user/" + USER_ID + "/profile.json";
2217

23-
18+
private static final String PROTECTED_RESOURCE_URL = "https://api.fitbit.com/1/user/%s/profile.json";
19+
2420
private FitbitApi20Example() {
2521
}
2622

2723
public static void main(String... args) throws Exception {
28-
24+
2925
// Replace these with your client id and secret fron your app
3026
final String clientId = "your client id";
3127
final String clientSecret = "your client secret";
3228
final OAuth20Service service = new ServiceBuilder(clientId)
3329
.apiSecret(clientSecret)
3430
.scope("activity profile") // replace with desired scope
35-
.callback("http://www.example.com/oauth_callback/") //your callback URL to store and handle the authorization code sent by Fitbit
31+
//your callback URL to store and handle the authorization code sent by Fitbit
32+
.callback("http://www.example.com/oauth_callback/")
3633
.state("some_params")
3734
.build(FitbitApi20.instance());
3835
final Scanner in = new Scanner(System.in);
@@ -53,21 +50,27 @@ public static void main(String... args) throws Exception {
5350

5451
// Trade the Request Token and Verfier for the Access Token
5552
System.out.println("Trading the Request Token for an Access Token...");
56-
final OAuth2AccessToken accessToken = service.getAccessToken(code);
53+
final OAuth2AccessToken oauth2AccessToken = service.getAccessToken(code);
5754
System.out.println("Got the Access Token!");
58-
System.out.println("(if your curious it looks like this: " + accessToken
59-
+ ", 'rawResponse'='" + accessToken.getRawResponse() + "')");
55+
System.out.println("(if your curious it looks like this: " + oauth2AccessToken
56+
+ ", 'rawResponse'='" + oauth2AccessToken.getRawResponse() + "')");
6057
System.out.println();
6158

59+
if (!(oauth2AccessToken instanceof FitBitOAuth2AccessToken)) {
60+
System.out.println("oauth2AccessToken is not instance of FitBitOAuth2AccessToken. Strange enough. exit.");
61+
System.exit(0);
62+
}
63+
64+
final FitBitOAuth2AccessToken accessToken = (FitBitOAuth2AccessToken) oauth2AccessToken;
6265
// Now let's go and ask for a protected resource!
6366
// This will get the profile for this user
6467
System.out.println("Now we're going to access a protected resource...");
6568

66-
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
69+
final OAuthRequest request = new OAuthRequest(Verb.GET,
70+
String.format(PROTECTED_RESOURCE_URL, accessToken.getUserId()));
6771
request.addHeader("x-li-format", "json");
6872

69-
//add header for authentication (why make it so complicated, Fitbit?)
70-
request.addHeader("Authorization", "Bearer " + accessToken.getAccessToken());
73+
service.signRequest(accessToken, request);
7174

7275
final Response response = service.execute(request);
7376
System.out.println();

0 commit comments

Comments
 (0)