|
| 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