Search in sources :

Example 16 with Credential

use of org.openstack4j.model.identity.v3.Credential in project openhab1-addons by openhab.

the class GCalEventDownloader method downloadEventFeed.

/**
     * Connects to Google-Calendar Service and returns the specified Events
     *
     * @return the corresponding Events or <code>null</code> if an error
     *         occurs. <i>Note:</i> We do only return events if their startTime lies between
     *         <code>now</code> and <code>now + 2 * refreshInterval</code> to reduce
     *         the amount of events to process.
     */
private static Events downloadEventFeed() {
    if (StringUtils.isBlank(calendar_name)) {
        logger.warn("Login aborted no calendar name defined");
        return null;
    }
    // authorization
    CalendarListEntry calendarID = GCalGoogleOAuth.getCalendarId(calendar_name);
    if (calendarID == null) {
        return null;
    }
    DateTime start = new DateTime(new Date(), TimeZone.getTimeZone(calendarID.getTimeZone()));
    DateTime end = new DateTime(new Date(start.getValue() + (2 * refreshInterval)), TimeZone.getTimeZone(calendarID.getTimeZone()));
    logger.debug("Downloading calendar feed for time interval: {} to  {} ", start, end);
    Events feed = null;
    try {
        Credential credential = GCalGoogleOAuth.getCredential(false);
        // set up global Calendar instance
        Calendar client = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName("openHAB").build();
        Calendar.Events.List l = client.events().list(calendarID.getId()).setSingleEvents(true).setTimeMin(start).setTimeMax(end);
        // add the fulltext filter if it has been configured
        if (StringUtils.isNotBlank(filter)) {
            l = l.setQ(filter);
        }
        feed = l.execute();
    } catch (IOException e1) {
        logger.error("Event fetch failed: {}", e1.getMessage());
    }
    try {
        if (feed != null) {
            checkIfFullCalendarFeed(feed.getItems());
        }
        return feed;
    } catch (Exception e) {
        logger.error("downloading CalendarEventFeed throws exception: {}", e.getMessage());
    }
    return null;
}
Also used : CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) Credential(com.google.api.client.auth.oauth2.Credential) Events(com.google.api.services.calendar.model.Events) Calendar(com.google.api.services.calendar.Calendar) TimeRangeCalendar(org.openhab.io.gcal.internal.util.TimeRangeCalendar) IOException(java.io.IOException) DateTime(com.google.api.client.util.DateTime) EventDateTime(com.google.api.services.calendar.model.EventDateTime) Date(java.util.Date) ConfigurationException(org.osgi.service.cm.ConfigurationException) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException)

Example 17 with Credential

use of org.openstack4j.model.identity.v3.Credential in project OpenRefine by OpenRefine.

the class FusionTableHandler method getFusionTablesService.

public static Fusiontables getFusionTablesService(String token) {
    Credential credential = new GoogleCredential().setAccessToken(token);
    Fusiontables fusiontables = new Fusiontables.Builder(GDataExtension.HTTP_TRANSPORT, GDataExtension.JSON_FACTORY, credential).setApplicationName(GDataExtension.SERVICE_APP_NAME).build();
    ;
    return fusiontables;
}
Also used : GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Credential(com.google.api.client.auth.oauth2.Credential) Fusiontables(com.google.api.services.fusiontables.Fusiontables) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential)

Example 18 with Credential

use of org.openstack4j.model.identity.v3.Credential in project api-samples by youtube.

the class YouTubeAnalyticsReports method main.

/**
     * This code authorizes the user, uses the YouTube Data API to retrieve
     * information about the user's YouTube channel, and then fetches and
     * prints statistics for the user's channel using the YouTube Analytics API.
     *
     * @param args command line args (not used).
     */
public static void main(String[] args) {
    // These scopes are required to access information about the
    // authenticated user's YouTube channel as well as Analytics
    // data for that channel.
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/yt-analytics.readonly", "https://www.googleapis.com/auth/youtube.readonly");
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "analyticsreports");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName("youtube-analytics-api-report-example").build();
        // This object is used to make YouTube Analytics API requests.
        analytics = new YouTubeAnalytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName("youtube-analytics-api-report-example").build();
        // Construct a request to retrieve the current user's channel ID.
        YouTube.Channels.List channelRequest = youtube.channels().list("id,snippet");
        channelRequest.setMine(true);
        channelRequest.setFields("items(id,snippet/title)");
        ChannelListResponse channels = channelRequest.execute();
        // List channels associated with the user.
        List<Channel> listOfChannels = channels.getItems();
        // The user's default channel is the first item in the list.
        Channel defaultChannel = listOfChannels.get(0);
        String channelId = defaultChannel.getId();
        PrintStream writer = System.out;
        if (channelId == null) {
            writer.println("No channel found.");
        } else {
            writer.println("Default Channel: " + defaultChannel.getSnippet().getTitle() + " ( " + channelId + " )\n");
            printData(writer, "Views Over Time.", executeViewsOverTimeQuery(analytics, channelId));
            printData(writer, "Top Videos", executeTopVideosQuery(analytics, channelId));
            printData(writer, "Demographics", executeDemographicsQuery(analytics, channelId));
        }
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
        e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}
Also used : PrintStream(java.io.PrintStream) Credential(com.google.api.client.auth.oauth2.Credential) Channel(com.google.api.services.youtube.model.Channel) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) YouTubeAnalytics(com.google.api.services.youtubeAnalytics.YouTubeAnalytics) ChannelListResponse(com.google.api.services.youtube.model.ChannelListResponse)

Example 19 with Credential

use of org.openstack4j.model.identity.v3.Credential in project api-samples by youtube.

the class AddSubscription method main.

/**
     * Subscribe the user's YouTube account to a user-selected channel.
     *
     * @param args command line args (not used).
     */
public static void main(String[] args) {
    // This OAuth 2.0 access scope allows for full read/write access to the
    // authenticated user's account.
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "addsubscription");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-addsubscription-sample").build();
        // We get the user selected channel to subscribe.
        // Retrieve the channel ID that the user is subscribing to.
        String channelId = getChannelId();
        System.out.println("You chose " + channelId + " to subscribe.");
        // Create a resourceId that identifies the channel ID.
        ResourceId resourceId = new ResourceId();
        resourceId.setChannelId(channelId);
        resourceId.setKind("youtube#channel");
        // Create a snippet that contains the resourceId.
        SubscriptionSnippet snippet = new SubscriptionSnippet();
        snippet.setResourceId(resourceId);
        // Create a request to add the subscription and send the request.
        // The request identifies subscription metadata to insert as well
        // as information that the API server should return in its response.
        Subscription subscription = new Subscription();
        subscription.setSnippet(snippet);
        YouTube.Subscriptions.Insert subscriptionInsert = youtube.subscriptions().insert("snippet,contentDetails", subscription);
        Subscription returnedSubscription = subscriptionInsert.execute();
        // Print information from the API response.
        System.out.println("\n================== Returned Subscription ==================\n");
        System.out.println("  - Id: " + returnedSubscription.getId());
        System.out.println("  - Title: " + returnedSubscription.getSnippet().getTitle());
    } catch (GoogleJsonResponseException e) {
        System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
        e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) SubscriptionSnippet(com.google.api.services.youtube.model.SubscriptionSnippet) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) ResourceId(com.google.api.services.youtube.model.ResourceId) Subscription(com.google.api.services.youtube.model.Subscription)

Example 20 with Credential

use of org.openstack4j.model.identity.v3.Credential in project api-samples by youtube.

the class Captions method main.

/**
     * Upload, list, update, download, and delete caption tracks.
     *
     * @param args command line args (not used).
     */
public static void main(String[] args) {
    // This OAuth 2.0 access scope allows for full read/write access to the
    // authenticated user's account and requires requests to use an SSL connection.
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.force-ssl");
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "captions");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-captions-sample").build();
        // Prompt the user to specify the action of the be achieved.
        String actionString = getActionFromUser();
        System.out.println("You chose " + actionString + ".");
        Action action = Action.valueOf(actionString.toUpperCase());
        switch(action) {
            case UPLOAD:
                uploadCaption(getVideoId(), getLanguage(), getName(), getCaptionFromUser());
                break;
            case LIST:
                listCaptions(getVideoId());
                break;
            case UPDATE:
                updateCaption(getCaptionIDFromUser(), getUpdateCaptionFromUser());
                break;
            case DOWNLOAD:
                downloadCaption(getCaptionIDFromUser());
                break;
            case DELETE:
                deleteCaption(getCaptionIDFromUser());
                break;
            default:
                // All the available methods are used in sequence just for the sake
                // of an example.
                //Prompt the user to specify a video to upload the caption track for and
                // a language, a name, a binary file for the caption track. Then upload the
                // caption track with the values that are selected by the user.
                String videoId = getVideoId();
                uploadCaption(videoId, getLanguage(), getName(), getCaptionFromUser());
                List<Caption> captions = listCaptions(videoId);
                if (captions.isEmpty()) {
                    System.out.println("Can't get video caption tracks.");
                } else {
                    // Retrieve the first uploaded caption track.
                    String firstCaptionId = captions.get(0).getId();
                    updateCaption(firstCaptionId, null);
                    downloadCaption(firstCaptionId);
                    deleteCaption(firstCaptionId);
                }
        }
    } catch (GoogleJsonResponseException e) {
        System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
        e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) Caption(com.google.api.services.youtube.model.Caption)

Aggregations

Credential (com.google.api.client.auth.oauth2.Credential)33 IOException (java.io.IOException)24 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)21 YouTube (com.google.api.services.youtube.YouTube)20 InputStreamContent (com.google.api.client.http.InputStreamContent)4 StoredCredential (com.google.api.client.auth.oauth2.StoredCredential)3 MediaHttpUploader (com.google.api.client.googleapis.media.MediaHttpUploader)3 MediaHttpUploaderProgressListener (com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener)3 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)2 HttpRequest (com.google.api.client.http.HttpRequest)2 DateTime (com.google.api.client.util.DateTime)2 Channel (com.google.api.services.youtube.model.Channel)2 ChannelListResponse (com.google.api.services.youtube.model.ChannelListResponse)2 Comment (com.google.api.services.youtube.model.Comment)2 CommentSnippet (com.google.api.services.youtube.model.CommentSnippet)2 CommentThread (com.google.api.services.youtube.model.CommentThread)2 CommentThreadListResponse (com.google.api.services.youtube.model.CommentThreadListResponse)2 Video (com.google.api.services.youtube.model.Video)2