简体   繁体   中英

Using Google API Objective-C client to construct MQL queries (Freebase)

Let me start by saying that I am new to MQL, Freebase, and the Google APIs.

I am attempting to get results from Freebase using the Google API Objective-C client, but I can't find any examples or information on using the API without generated classes.

I've found this page http://code.google.com/p/google-api-objectivec-client/wiki/Introduction

But the section "Using APIs Without Generated Classes" doesn't give me any relevant information on constructing these queries and the examples included are all generated classes.

So far, I've found that I need to first create a GTLService object with an RPC URL (I'm guessing that's https://www.googleapis.com/freebase ), set the API Version (v1sandbox for the sandbox environment), and set the API Key (kGoogleAPIKey in this case).

GTLService * service = [[GTLService alloc] init];
    service.rpcURL = [NSURL URLWithString:@"https://www.googleapis.com/freebase"];
    service.apiVersion = @"v1sandbox";
    service.APIKey = kGoogleAPIKey;

Done! Awesome, no problem.

The next part is where I'm stuck. My question is, how do I construct an MQL query using the Google API Objective-C client to retrieve results from Freebase?

In the section "Objects and Queries", from the link above, it states that I need to create a query and execute it, but where do I include the MQL query?

// queryWithMethodName: methodName is the RPC method name to use
GTLQuery * query = [GTLQuery queryWithMethodName:@"mqlread"]; // Not sure if this is correct
GTLServiceTicket * ticket = [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
    NSArray * items = [object items];
    NSLog(@"%@", [items description]);
    // Do something with items.
}];

For reference, the Freebase API URL is

https://www.googleapis.com/freebase/v1/mqlread?query={}

and the MQL query is

[{
"id":   null,
"name": null,
"type": "/travel/travel_destination",
"/travel/travel_destination/tourist_attractions": [{
   "id":   null,
   "name": null
}],
"/location/location/containedby": [{
   "name": "California"
}]
}]

I'd really appreciate any help or even a point in the right direction!

I'm not sure why you can't use the generated classes, but there are some fundamental misconceptions in your current approach.

Most important is that the RPC API is a separate API endpoint from the others (such as Freebase). I'm also pretty sure that MQLRead is incompatible with the RPC API because it's return results don't have a deterministic shape. If it were compatible, the method name would be freebase.mqlread, not mqlread.

If you really can't use the generate classes for some reason, I'd fall back to constructing the URLs yourself. All you need to do is take your query as a JSON object and serialize it as the query parameter, then add any other parameters such as your API key. The results are all JSON too, so just deserialize them into JSON objects that you can work with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM