001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *      André Justo
016 */
017package org.nuxeo.ecm.media.publishing.youtube;
018
019import com.google.api.client.http.GenericUrl;
020import com.google.api.client.http.HttpRequestFactory;
021import com.google.api.client.http.HttpResponse;
022import com.google.api.client.json.GenericJson;
023import com.google.api.client.json.JsonObjectParser;
024import org.nuxeo.ecm.platform.oauth2.providers.AbstractOAuth2UserEmailProvider;
025
026import java.io.IOException;
027
028/**
029 * @since 7.3
030 */
031public class YoutubeOAuth2ServiceProvider extends AbstractOAuth2UserEmailProvider {
032
033    private static final String TOKEN_INFO_URL = "https://www.googleapis.com/oauth2/v1/tokeninfo";
034
035    private static final HttpRequestFactory requestFactory =
036        HTTP_TRANSPORT.createRequestFactory(request -> request.setParser(new JsonObjectParser(JSON_FACTORY)));
037
038    @Override
039    protected String getUserEmail(String accessToken) throws IOException {
040        GenericUrl url = new GenericUrl(TOKEN_INFO_URL);
041        url.set("access_token", accessToken);
042
043        HttpResponse response = requestFactory.buildGetRequest(url).execute();
044        GenericJson json = response.parseAs(GenericJson.class);
045        return (String) json.get("email");
046    }
047}