Skip to content

Commit 2d4ecef

Browse files
committed
round trip working example using sickle cell anemia
1 parent 68d2628 commit 2d4ecef

File tree

6 files changed

+283
-0
lines changed

6 files changed

+283
-0
lines changed

samples/ttam/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mvn compile && mvn -q exec:java -Dexec.args="client_id client_secret"

samples/ttam/pom.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.google.oauth-client</groupId>
6+
<artifactId>google-oauth-client-parent</artifactId>
7+
<version>1.23.0-SNAPSHOT</version>
8+
<relativePath>../../pom.xml</relativePath>
9+
</parent>
10+
<artifactId>simple-example-23andMe</artifactId>
11+
<name>23andMe API Example using OAuth2</name>
12+
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.codehaus.mojo</groupId>
17+
<artifactId>exec-maven-plugin</artifactId>
18+
<version>1.1</version>
19+
<executions>
20+
<execution>
21+
<goals>
22+
<goal>java</goal>
23+
</goals>
24+
</execution>
25+
</executions>
26+
<configuration>
27+
<mainClass>com.google.api.services.samples.ttam.SimpleExample</mainClass>
28+
</configuration>
29+
</plugin>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-deploy-plugin</artifactId>
33+
<version>2.5</version>
34+
<configuration>
35+
<skip>true</skip>
36+
</configuration>
37+
</plugin>
38+
<plugin>
39+
<artifactId>maven-checkstyle-plugin</artifactId>
40+
<version>2.6</version>
41+
<configuration>
42+
<configLocation>../checkstyle.xml</configLocation>
43+
<consoleOutput>true</consoleOutput>
44+
<failOnViolation>false</failOnViolation>
45+
</configuration>
46+
<executions>
47+
<execution>
48+
<goals>
49+
<goal>check</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
</plugins>
55+
<finalName>${project.artifactId}-${project.version}</finalName>
56+
</build>
57+
<dependencies>
58+
<dependency>
59+
<groupId>com.google.oauth-client</groupId>
60+
<artifactId>google-oauth-client</artifactId>
61+
</dependency>
62+
<dependency>
63+
<groupId>com.google.oauth-client</groupId>
64+
<artifactId>google-oauth-client-java6</artifactId>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.google.oauth-client</groupId>
68+
<artifactId>google-oauth-client-jetty</artifactId>
69+
</dependency>
70+
<dependency>
71+
<groupId>com.google.http-client</groupId>
72+
<artifactId>google-http-client-jackson2</artifactId>
73+
</dependency>
74+
</dependencies>
75+
<properties>
76+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
77+
</properties>
78+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2011 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
16+
package com.google.api.services.samples.ttam;
17+
18+
import com.google.api.client.util.Key;
19+
20+
import java.util.List;
21+
22+
public class Account {
23+
24+
@Key
25+
public List<Profile> profiles;
26+
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2011 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
16+
package com.google.api.services.samples.ttam;
17+
18+
import com.google.api.client.util.Key;
19+
20+
public class Genotype {
21+
22+
@Key
23+
public String i3003137;
24+
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2011 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
16+
package com.google.api.services.samples.ttam;
17+
18+
import com.google.api.client.util.Key;
19+
20+
public class Profile {
21+
22+
@Key
23+
public String id;
24+
25+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) 2011 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.services.samples.ttam;
16+
17+
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
18+
import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
19+
import com.google.api.client.auth.oauth2.Credential;
20+
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
21+
import com.google.api.client.extensions.java6.auth.oauth2.VerificationCodeReceiver;
22+
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
23+
import com.google.api.client.http.*;
24+
import com.google.api.client.http.javanet.NetHttpTransport;
25+
import com.google.api.client.json.JsonFactory;
26+
import com.google.api.client.json.JsonObjectParser;
27+
import com.google.api.client.json.jackson2.JacksonFactory;
28+
import com.google.api.client.util.store.DataStoreFactory;
29+
import com.google.api.client.util.store.FileDataStoreFactory;
30+
31+
import java.io.File;
32+
import java.io.IOException;
33+
import java.util.Arrays;
34+
import java.util.Collection;
35+
36+
import static com.google.api.client.auth.oauth2.BearerToken.authorizationHeaderAccessMethod;
37+
38+
39+
/**
40+
* 23andMe API Example with Sickle Cell Anemia, using OAuth2
41+
*
42+
* Instructions:
43+
* 1) Create a 23andMe client at https://api.23andme.com/apply/ with a redirect_uri of http://127.0.0.1:8080/Callback
44+
* 2) Run this java application with two arguments: the client_id followed by the client_secret
45+
* 3) Authenticate, using your 23andMe credentials
46+
*
47+
* @author Dennis Byrne <[email protected]>
48+
*/
49+
public class SimpleExample {
50+
51+
/**
52+
* The client_id field value at https://api.23andme.com/dev/
53+
*/
54+
private final String clientId;
55+
/**
56+
* The client_secret field value at https://api.23andme.com/dev/
57+
*/
58+
private final String clientSecret;
59+
private final DataStoreFactory dataStoreFactory;
60+
private final HttpTransport httpTransport;
61+
private final JsonFactory jsonFactory;
62+
63+
private static final Collection<String> SCOPES = Arrays.asList("basic", "i3003137");
64+
65+
private static final String DOMAIN = "127.0.0.1";
66+
private static final int PORT = 8080;
67+
private static final String BASE_URL = "https://api.23andme.com/";
68+
private static final String AUTHORIZATION_SERVER_URL = BASE_URL + "authorize/";
69+
private static final String TOKEN_SERVER_URL = BASE_URL + "token/";
70+
71+
public SimpleExample(FileDataStoreFactory dataStoreFactory, String clientId, String clientSecret) {
72+
this.clientId = clientId;
73+
this.dataStoreFactory = dataStoreFactory;
74+
this.clientSecret = clientSecret;
75+
this.httpTransport = new NetHttpTransport();
76+
this.jsonFactory = new JacksonFactory();
77+
}
78+
79+
public static void main(String[] args) throws Throwable {
80+
File directory = new File(System.getProperty("user.home"), ".store/ttam");
81+
if (args.length != 2) {
82+
throw new AssertionError("client_id and client_secret are required");
83+
}
84+
SimpleExample simpleExample = new SimpleExample(new FileDataStoreFactory(directory), args[0], args[1]);
85+
Genotype genotype = simpleExample.retrieveGenotype();
86+
System.out.println("Genotype @ i3003137, a SNP associated with Sickle Cell Anemia: " + genotype.i3003137);
87+
}
88+
89+
public Genotype retrieveGenotype() throws Exception {
90+
final Credential credential = authorize();
91+
92+
HttpRequestInitializer initializer = new HttpRequestInitializer() {
93+
public void initialize(HttpRequest request) throws IOException {
94+
credential.initialize(request);
95+
request.setParser(new JsonObjectParser(jsonFactory));
96+
}
97+
};
98+
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(initializer);
99+
100+
GenericUrl userUrl = new GenericUrl(BASE_URL + "/1/user/");
101+
HttpRequest userRequest = requestFactory.buildGetRequest(userUrl);
102+
HttpResponse userResponse = userRequest.execute();
103+
Account account = userResponse.parseAs(Account.class);
104+
Profile profile = account.profiles.get(0);
105+
106+
GenericUrl genotypesUrl = new GenericUrl(BASE_URL + "1/genotypes/" + profile.id + "/?locations=i3003137");
107+
HttpRequest genotypesRequest = requestFactory.buildGetRequest(genotypesUrl);
108+
HttpResponse genotypesResponse = genotypesRequest.execute();
109+
return genotypesResponse.parseAs(Genotype.class);
110+
}
111+
112+
private Credential authorize() throws Exception {
113+
HttpExecuteInterceptor credentials = new ClientParametersAuthentication(this.clientId, this.clientSecret);
114+
AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(authorizationHeaderAccessMethod(),
115+
httpTransport,
116+
jsonFactory,
117+
new GenericUrl(TOKEN_SERVER_URL),
118+
credentials,
119+
this.clientId,
120+
AUTHORIZATION_SERVER_URL)
121+
.setScopes(SCOPES)
122+
.setDataStoreFactory(dataStoreFactory).build();
123+
VerificationCodeReceiver receiver = new LocalServerReceiver.Builder().setHost(DOMAIN)
124+
.setPort(PORT).build();
125+
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
126+
}
127+
}

0 commit comments

Comments
 (0)