From a7c8a166d7d654b619d7723c6d1846ecfad102bb Mon Sep 17 00:00:00 2001 From: Dennis Byrne Date: Wed, 26 Apr 2017 17:30:12 -0700 Subject: [PATCH] squashing all commits in order to erase CLAless email address from the first commit --- .../oauth2-with-23andme-in-java/index.md | 31 +++++ .../oauth2-with-23andme-in-java/java/pom.xml | 86 ++++++++++++ .../api/services/samples/ttam/Account.java | 27 ++++ .../api/services/samples/ttam/Genotype.java | 25 ++++ .../api/services/samples/ttam/Profile.java | 25 ++++ .../services/samples/ttam/SimpleExample.java | 128 ++++++++++++++++++ 6 files changed, 322 insertions(+) create mode 100644 tutorials/oauth2-with-23andme-in-java/index.md create mode 100644 tutorials/oauth2-with-23andme-in-java/java/pom.xml create mode 100644 tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/Account.java create mode 100644 tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/Genotype.java create mode 100644 tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/Profile.java create mode 100644 tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/SimpleExample.java diff --git a/tutorials/oauth2-with-23andme-in-java/index.md b/tutorials/oauth2-with-23andme-in-java/index.md new file mode 100644 index 0000000000..fede957e7e --- /dev/null +++ b/tutorials/oauth2-with-23andme-in-java/index.md @@ -0,0 +1,31 @@ +--- +title: Authenticating to the 23andMe API with the Google Java OAuth 2.0 Client +description: Authenticate to the 23andMe API with the Google Java OAuth 2.0 client and pull a single genotype for i3003137, a SNP associated with Sickle Cell Anemia. +author: dennisbyrne +tags: OAuth, OAuth2, Java, 23andMe, genomics +date_published: 2017-04-26 +--- + +This sample app uses Google's OAuth 2.0 java lib to authenticate with 23andMe. Once authenticated the app pulls a single genotype for i3003137, a SNP associated with Sickle Cell Anemia. + +## Create a 23andMe client + +Follow the instructions at https://api.23andme.com/apply/ to create a 23andMe Oauth client. Make sure to set your `redirect_uri` to http://127.0.0.1:8080/Callback . You will be given a `client_id` and a `client_secret`. + +## Clone the community repo + + git clone https://github.com/GoogleCloudPlatform/community.git + +## Run the sample app + + cd community/tutorials/oauth2-with-23andMe-in-java/java/ + +Make sure you have [maven installed][install_maven], then run the app: + + mvn compile && mvn -q exec:java -Dexec.args="`client_id` `client_secret`" + +[install_maven]: https://maven.apache.org/install.html + +## Example output + +`Genotype @ i3003137, a SNP associated with Sickle Cell Anemia: TT` diff --git a/tutorials/oauth2-with-23andme-in-java/java/pom.xml b/tutorials/oauth2-with-23andme-in-java/java/pom.xml new file mode 100644 index 0000000000..52f9258da9 --- /dev/null +++ b/tutorials/oauth2-with-23andme-in-java/java/pom.xml @@ -0,0 +1,86 @@ + + + + 4.0.0 + + com.google.oauth-client + google-oauth-client-parent + 1.23.0-SNAPSHOT + ../../pom.xml + + simple-example-23andMe + 23andMe API Example using OAuth2 + + + + + org.codehaus.mojo + exec-maven-plugin + 1.1 + + + + java + + + + + com.google.api.services.samples.ttam.SimpleExample + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.5 + + true + + + + maven-checkstyle-plugin + 2.6 + + ../checkstyle.xml + true + false + + + + + check + + + + + + ${project.artifactId}-${project.version} + + + + com.google.oauth-client + google-oauth-client-jetty + + + com.google.http-client + google-http-client-jackson2 + + + + UTF-8 + + diff --git a/tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/Account.java b/tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/Account.java new file mode 100644 index 0000000000..c1c9ca1539 --- /dev/null +++ b/tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/Account.java @@ -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 profiles; + +} diff --git a/tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/Genotype.java b/tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/Genotype.java new file mode 100644 index 0000000000..de82963f66 --- /dev/null +++ b/tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/Genotype.java @@ -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; + +} diff --git a/tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/Profile.java b/tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/Profile.java new file mode 100644 index 0000000000..074847d18c --- /dev/null +++ b/tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/Profile.java @@ -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; + +} diff --git a/tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/SimpleExample.java b/tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/SimpleExample.java new file mode 100644 index 0000000000..f35d52e6a2 --- /dev/null +++ b/tutorials/oauth2-with-23andme-in-java/java/src/main/java/com/google/api/services/samples/ttam/SimpleExample.java @@ -0,0 +1,128 @@ +/* + * 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 + */ +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 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(); + LocalServerReceiver.Builder builder = new LocalServerReceiver.Builder(); + VerificationCodeReceiver receiver = builder.setHost(DOMAIN) + .setPort(PORT).build(); + return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); + } +}