Saturday, April 18, 2015

Youtube data client library iOS, oauth2, HTTPfetcher

1. Compiling google api

This page shows the method to compile the google api to a project. I used the second way: compiling the source files directly into iOS project.

When I downloaded its source file, the oauth2 and HTTPfetcher are not there. We need to download them separately from here and here. Then just follow the procedure in the first page to finish compiling.

2. Youtube search

To search the data on youtube, firstly, import three headers:

#import "GTLServiceYouTube.h"
#import "GTLQueryYouTube.h"
#import "GTLYouTubeSearchListResponse.h"

Then for example, we search here for the youtube id by giving a searching word string:

GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = yourAPIKey;
GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"id"];
query.type = @"video";
query.q = aSearchingString;
query.maxResults = aInt;

The code is very clear itself. We declare a youtube service instance, give it the APIKey, declare a query instance. The meaning of type, q and maxResults can be found here. Finally we execute this query which will give us a ticket. In the completionHandler, we receive the returned object.

GTLServiceTicket *ticketSend = [service executeQuery:query
                                   completionHandler:^(GTLServiceTicket *ticketReceive, id object, NSError *error)
        if (!error) {
            GTLYouTubeSearchListResponse *items = object;
        } else {