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.wistia;
018
019import com.google.api.client.http.GenericUrl;
020import com.google.api.client.http.HttpRequest;
021import com.google.api.client.http.HttpRequestFactory;
022import com.google.api.client.http.HttpRequestInitializer;
023import com.google.api.client.http.HttpResponse;
024import com.google.api.client.json.GenericJson;
025import com.google.api.client.json.JsonObjectParser;
026import org.nuxeo.ecm.platform.oauth2.providers.AbstractOAuth2UserEmailProvider;
027
028import java.io.IOException;
029
030/**
031 * @since 7.3
032 */
033public class WistiaOAuth2ServiceProvider extends AbstractOAuth2UserEmailProvider {
034
035    private static final String ACCOUNT_INFO_URL = "https://api.wistia.com/v1/account.json";
036
037    private static final HttpRequestFactory requestFactory =
038        HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
039            @Override
040            public void initialize(HttpRequest request) throws IOException {
041                request.setParser(new JsonObjectParser(JSON_FACTORY));
042            }
043        });
044
045    @Override
046    protected String getUserEmail(String accessToken) throws IOException {
047        GenericUrl url = new GenericUrl(ACCOUNT_INFO_URL);
048        url.set("api_password", accessToken);
049
050        HttpResponse response = requestFactory.buildGetRequest(url).execute();
051        GenericJson json = response.parseAs(GenericJson.class);
052        return (String) json.get("name");
053    }
054}