-
Notifications
You must be signed in to change notification settings - Fork 279
23andMe API & google oauth2 - round trip working example w/ sickle cell anemia #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| mvn compile && mvn -q exec:java -Dexec.args="client_id client_secret" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>com.google.oauth-client</groupId> | ||
| <artifactId>google-oauth-client-parent</artifactId> | ||
| <version>1.23.0-SNAPSHOT</version> | ||
| <relativePath>../../pom.xml</relativePath> | ||
| </parent> | ||
| <artifactId>simple-example-23andMe</artifactId> | ||
| <name>23andMe API Example using OAuth2</name> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.codehaus.mojo</groupId> | ||
| <artifactId>exec-maven-plugin</artifactId> | ||
| <version>1.1</version> | ||
| <executions> | ||
| <execution> | ||
| <goals> | ||
| <goal>java</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| <configuration> | ||
| <mainClass>com.google.api.services.samples.ttam.SimpleExample</mainClass> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-deploy-plugin</artifactId> | ||
| <version>2.5</version> | ||
| <configuration> | ||
| <skip>true</skip> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <artifactId>maven-checkstyle-plugin</artifactId> | ||
| <version>2.6</version> | ||
| <configuration> | ||
| <configLocation>../checkstyle.xml</configLocation> | ||
| <consoleOutput>true</consoleOutput> | ||
| <failOnViolation>false</failOnViolation> | ||
| </configuration> | ||
| <executions> | ||
| <execution> | ||
| <goals> | ||
| <goal>check</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| <finalName>${project.artifactId}-${project.version}</finalName> | ||
| </build> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.oauth-client</groupId> | ||
| <artifactId>google-oauth-client</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.oauth-client</groupId> | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| <artifactId>google-oauth-client-java6</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.oauth-client</groupId> | ||
| <artifactId>google-oauth-client-jetty</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.http-client</groupId> | ||
| <artifactId>google-http-client-jackson2</artifactId> | ||
| </dependency> | ||
| </dependencies> | ||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
| </project> | ||
27 changes: 27 additions & 0 deletions
27
samples/ttam/src/main/java/com/google/api/services/samples/ttam/Account.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright (c) 2011 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License | ||
| * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
| * or implied. See the License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
|
|
||
|
|
||
| package com.google.api.services.samples.ttam; | ||
|
|
||
| import com.google.api.client.util.Key; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Account { | ||
|
|
||
| @Key | ||
| public List<Profile> profiles; | ||
|
|
||
| } |
25 changes: 25 additions & 0 deletions
25
samples/ttam/src/main/java/com/google/api/services/samples/ttam/Genotype.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Copyright (c) 2011 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License | ||
| * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
| * or implied. See the License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
|
|
||
|
|
||
| package com.google.api.services.samples.ttam; | ||
|
|
||
| import com.google.api.client.util.Key; | ||
|
|
||
| public class Genotype { | ||
|
|
||
| @Key | ||
| public String i3003137; | ||
|
|
||
| } |
25 changes: 25 additions & 0 deletions
25
samples/ttam/src/main/java/com/google/api/services/samples/ttam/Profile.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Copyright (c) 2011 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License | ||
| * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
| * or implied. See the License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
|
|
||
|
|
||
| package com.google.api.services.samples.ttam; | ||
|
|
||
| import com.google.api.client.util.Key; | ||
|
|
||
| public class Profile { | ||
|
|
||
| @Key | ||
| public String id; | ||
|
|
||
| } |
127 changes: 127 additions & 0 deletions
127
samples/ttam/src/main/java/com/google/api/services/samples/ttam/SimpleExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Copyright (c) 2011 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License | ||
| * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
| * or implied. See the License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
|
|
||
| package com.google.api.services.samples.ttam; | ||
|
|
||
| import com.google.api.client.auth.oauth2.AuthorizationCodeFlow; | ||
| import com.google.api.client.auth.oauth2.ClientParametersAuthentication; | ||
| import com.google.api.client.auth.oauth2.Credential; | ||
| import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; | ||
| import com.google.api.client.extensions.java6.auth.oauth2.VerificationCodeReceiver; | ||
| import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; | ||
| import com.google.api.client.http.*; | ||
| import com.google.api.client.http.javanet.NetHttpTransport; | ||
| import com.google.api.client.json.JsonFactory; | ||
| import com.google.api.client.json.JsonObjectParser; | ||
| import com.google.api.client.json.jackson2.JacksonFactory; | ||
| import com.google.api.client.util.store.DataStoreFactory; | ||
| import com.google.api.client.util.store.FileDataStoreFactory; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
|
|
||
| import static com.google.api.client.auth.oauth2.BearerToken.authorizationHeaderAccessMethod; | ||
|
|
||
|
|
||
| /** | ||
| * 23andMe API Example with Sickle Cell Anemia, using OAuth2 | ||
| * | ||
| * Instructions: | ||
| * 1) Create a 23andMe client at https://api.23andme.com/apply/ with a redirect_uri of http://127.0.0.1:8080/Callback | ||
| * 2) Run this java application with two arguments: the client_id followed by the client_secret | ||
| * 3) Authenticate, using your 23andMe credentials | ||
| * | ||
| * @author Dennis Byrne <[email protected]> | ||
| */ | ||
| public class SimpleExample { | ||
|
|
||
| /** | ||
| * The client_id field value at https://api.23andme.com/dev/ | ||
| */ | ||
| private final String clientId; | ||
| /** | ||
| * The client_secret field value at https://api.23andme.com/dev/ | ||
| */ | ||
| private final String clientSecret; | ||
| private final DataStoreFactory dataStoreFactory; | ||
| private final HttpTransport httpTransport; | ||
| private final JsonFactory jsonFactory; | ||
|
|
||
| private static final Collection<String> SCOPES = Arrays.asList("basic", "i3003137"); | ||
|
|
||
| private static final String DOMAIN = "127.0.0.1"; | ||
| private static final int PORT = 8080; | ||
| private static final String BASE_URL = "https://api.23andme.com/"; | ||
| private static final String AUTHORIZATION_SERVER_URL = BASE_URL + "authorize/"; | ||
| private static final String TOKEN_SERVER_URL = BASE_URL + "token/"; | ||
|
|
||
| public SimpleExample(FileDataStoreFactory dataStoreFactory, String clientId, String clientSecret) { | ||
| this.clientId = clientId; | ||
| this.dataStoreFactory = dataStoreFactory; | ||
| this.clientSecret = clientSecret; | ||
| this.httpTransport = new NetHttpTransport(); | ||
| this.jsonFactory = new JacksonFactory(); | ||
| } | ||
|
|
||
| public static void main(String[] args) throws Throwable { | ||
| File directory = new File(System.getProperty("user.home"), ".store/ttam"); | ||
| if (args.length != 2) { | ||
| throw new AssertionError("client_id and client_secret are required"); | ||
| } | ||
| SimpleExample simpleExample = new SimpleExample(new FileDataStoreFactory(directory), args[0], args[1]); | ||
| Genotype genotype = simpleExample.retrieveGenotype(); | ||
| System.out.println("Genotype @ i3003137, a SNP associated with Sickle Cell Anemia: " + genotype.i3003137); | ||
| } | ||
|
|
||
| public Genotype retrieveGenotype() throws Exception { | ||
| final Credential credential = authorize(); | ||
|
|
||
| HttpRequestInitializer initializer = new HttpRequestInitializer() { | ||
| public void initialize(HttpRequest request) throws IOException { | ||
| credential.initialize(request); | ||
| request.setParser(new JsonObjectParser(jsonFactory)); | ||
| } | ||
| }; | ||
| HttpRequestFactory requestFactory = httpTransport.createRequestFactory(initializer); | ||
|
|
||
| GenericUrl userUrl = new GenericUrl(BASE_URL + "/1/user/"); | ||
| HttpRequest userRequest = requestFactory.buildGetRequest(userUrl); | ||
| HttpResponse userResponse = userRequest.execute(); | ||
| Account account = userResponse.parseAs(Account.class); | ||
| Profile profile = account.profiles.get(0); | ||
|
|
||
| GenericUrl genotypesUrl = new GenericUrl(BASE_URL + "1/genotypes/" + profile.id + "/?locations=i3003137"); | ||
| HttpRequest genotypesRequest = requestFactory.buildGetRequest(genotypesUrl); | ||
| HttpResponse genotypesResponse = genotypesRequest.execute(); | ||
| return genotypesResponse.parseAs(Genotype.class); | ||
| } | ||
|
|
||
| private Credential authorize() throws Exception { | ||
| HttpExecuteInterceptor credentials = new ClientParametersAuthentication(this.clientId, this.clientSecret); | ||
| AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(authorizationHeaderAccessMethod(), | ||
| httpTransport, | ||
| jsonFactory, | ||
| new GenericUrl(TOKEN_SERVER_URL), | ||
| credentials, | ||
| this.clientId, | ||
| AUTHORIZATION_SERVER_URL) | ||
| .setScopes(SCOPES) | ||
| .setDataStoreFactory(dataStoreFactory).build(); | ||
| VerificationCodeReceiver receiver = new LocalServerReceiver.Builder().setHost(DOMAIN) | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| .setPort(PORT).build(); | ||
| return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.