001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *      André Justo
018 */
019package org.nuxeo.ecm.media.publishing.wistia;
020
021import com.google.api.client.http.GenericUrl;
022import com.google.api.client.http.HttpRequest;
023import com.google.api.client.http.HttpRequestFactory;
024import com.google.api.client.http.HttpRequestInitializer;
025import com.google.api.client.http.HttpResponse;
026import com.google.api.client.json.GenericJson;
027import com.google.api.client.json.JsonObjectParser;
028import org.nuxeo.ecm.platform.oauth2.providers.AbstractOAuth2UserEmailProvider;
029
030import java.io.IOException;
031
032/**
033 * @since 7.3
034 */
035public class WistiaOAuth2ServiceProvider extends AbstractOAuth2UserEmailProvider {
036
037    private static final String ACCOUNT_INFO_URL = "https://api.wistia.com/v1/account.json";
038
039    private static final HttpRequestFactory requestFactory =
040        HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
041            @Override
042            public void initialize(HttpRequest request) throws IOException {
043                request.setParser(new JsonObjectParser(JSON_FACTORY));
044            }
045        });
046
047    @Override
048    protected String getUserEmail(String accessToken) throws IOException {
049        GenericUrl url = new GenericUrl(ACCOUNT_INFO_URL);
050        url.set("api_password", accessToken);
051
052        HttpResponse response = requestFactory.buildGetRequest(url).execute();
053        GenericJson json = response.parseAs(GenericJson.class);
054        return (String) json.get("name");
055    }
056}