I have generated an API Key using google's console for my project. Then as they have mentioned in their documentation, I have added the key in my applications AppDelegate in the didFinishLaunchingWithOptions method. The following code:
[GMSServices provideAPIKey:#"{MY_API_KEY}"];
Then I used the following code to send a Google Places Auto complete request from inside my ViewController:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%#&sensor=false&key={MY_API_KEY}",searchString]]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:120];
placesListConnection = [NSURLConnection connectionWithRequest:request delegate:self];
And I am getting the following response:
{
"error_message" : "This IP, site or mobile application is not authorized to use this API key.",
"predictions" : [],
"status" : "REQUEST_DENIED"
}
What am I doing wrong here? How to rectify this?
Finally solved the issue. Instead of using API_KEY for IOS, i created an API_KEY for web that doesn't need a referrer and then sent the request using that key. Now it is working perfectly.
Have you checked Developers Console? Is Places API enabled under APIs & auth > APIs?
Related
How do you add custom headers when executing API calls with Cloud Endpoints in an iOS app (with Swift)?
Here is the same question answered for Android, but I cannot find an iOS example anywhere:
Modify HTTP headers in Google App Engine Endpoints (Android)
Thanks!
You can use the additionalHTTPHeaders property associated with each GTLService base class. For e.g.
var service = GTLServiceTictactoe()
service.retryEnabled = YES
service.additionalHTTPHeaders = ["my custom header name " : "my custom header value"]
....
Here is the description of this property from documentation
/**
* Any additional HTTP headers for this queries executed by this service.
*
* Individual queries may have additionalHTTPHeaders specified as well.
*/
#property(atomic, copy, nullable) NSDictionary<NSString *, NSString *> *additionalHTTPHeaders;
I'm not familiar with google app engine but I took a look at their iOS example app tic tac toe. So there is nothing complicated there. They are using standard iOS HTTP request.
you can find the following lines here
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:infoURL];
NSString *userAgent = [auth userAgent];
[request setValue:userAgent forHTTPHeaderField:#"User-Agent"];
[request setValue:#"no-cache" forHTTPHeaderField:#"Cache-Control"];
after adding the header to NSMutableURLRequest then you can use this request with NSURLConnection.
so if you need something additional you can add it there. here is a good tutorial if you are new to iOS development.
I've tried checking on stackoverflow and on the api docs, but couldn't find any info pertaining to this particular question.
What I'm trying to do is getting the public folder contents of a particular dropbox account (like the /metadata function of the api), without the need to authorize/link to dropbox.
I know "/metadata/link" allows you to get metadata of a particular link without user authorization, but I couldn't find a way to get regular metadata and file listing...
Actually it doesn't even need to be a public folder (as I've read recently that dropbox discourages developers regarding relying on public folders for apps), it can just as well be a regular folder through its shared link or anything of the kind...
I'm using api v1 (but can consider changing to v2 if this is impossible with v1), although I'm not even sure if this is at all possible.
Thank you!
For future reference, if anyone needs this as well, here's how I did it:
NSString *parameters = [NSString stringWithFormat:#"?link=%#&client_id=%#&client_secret=%#",fileUrl, appKey, appSecret];
NSString *sharedPath = [NSString stringWithFormat:#"%#/%#",#"https://api.dropbox.com/1/metadata/link", parameters];
NSURL *url = [NSURL URLWithString:sharedPath];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10];
theRequest.timeoutInterval = 5.0;
theRequest.HTTPMethod = #"POST";
NSData * resData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
Use NSJSONSerialization to parse resData and refer to the api documentation for json fields, and that's it.
For the sake of simplicity showing it here I used synchronous request, but it can just as easily be done with asynchronous connection delegates.
I am using the Objective C library for LinkedIn and trying to fetch user's primary emailId.
I am doing following :-
1)https://api.linkedin.com/uas/oauth/requestToken?scope=r_emailaddress >> Passing this as the url while requesting for request token.
2) And http://api.linkedin.com/v1/people/~:(id,first-name,industry,email-address) while fetching profile for user but
I never received email address.
Can you please let me know, what am I missing.
Thanks in advance
I am able to get the email address successfully. Only change was in the request method for requestToken to pass the scope parameter while requesting for permission in a different way not described in the reference guide on LinkedIn developer page.
Its in "RDLinkedInEngine.m" file >> "sendTokenRequestWithURL:token:onSuccess:onFailure" method >>
OARequestParameter *nameParam = [[OARequestParameter alloc] initWithName:#"scope" value:#"r_basicprofile+rw_nus+r_emailaddress"];
NSArray *params = [NSArray arrayWithObjects:nameParam, nil];
[request setParameters:params];
OARequestParameter * scopeParameter=[OARequestParameter requestParameterWithName:#"scope"
value:#"r_emailaddress r_fullprofile rw_nus"];
[request setParameters:[NSArray arrayWithObject:scopeParameter]];
Make sure that while requesting for access token , you keep the method as it is provided by default because the same method is used to get request token as well as access token.
I am building an iPhone App using PhoneGap. I am using ChildBrowser plugin.
If I have a form in the App with username/pass, is there anyway I can post those information to an URL like www.mydomain.com/login.php and open it in ChildBrowser?
Please let me know.
Thanks.
You can certainly pass GET parameters to childBrowser.showWebPage().
You could intercept the submit of your form in your app and gather up the field names and values passed (assuming this is a login form, so say username and password) and pass them as parameters to the URL opened in ChildBrowser.
childBrowser.showWebPage('https://www.mydomain.com/login.php?username='+username+'&password='+password)
This is a rather unsophisticated version for demonstration purposes. It wouldn't take much to make it better.
The catch is of course that it is being sent via GET. If your login form is expecting POST and you can't change that, you could have a problem.
Edit:
If POST is your only option, perhaps you would be better off using AJAX to post to the form instead of using child browser.
I was looking for a solution, and ended developing it. This doesn't come out of the box in childbrowser for iPhone.
So, here the code that I added to the plugin.
FYI, data should be a string with the format: arg1=value1&arg2=value2&foo=bar
ChildbrowserViewController
I've added the following method there:
- (void)postURL:(NSString*)url data:(NSString*)data{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: [NSURL URLWithString:url]];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [data dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
webView.hidden = NO;}
ChildbrowserCommand
I've added the following code in the showWebPage method:
NSString* method = [ options objectForKey:#"method"];
if ([method isEqualToString:#"POST"]){
[childBrowser postURL:url data:[options objectForKey:#"data"]];
}else{
[childBrowser loadURL:url];
}
HTH!
Milton.
I'm trying to connect to Google RESTful API's but all I get went doing so is a login screen.
My goal is to be able to create a Calendar in Google Calendars using RESTful.
I already created my API keys on Google APIs Console.
I did this:
NSString* baseURL = #"https://accounts.google.com/o/oauth2/auth?";
NSString* url = [NSString stringWithFormat:#"%#%#",baseURL,[self dictionaryToURLStringVariables:[self authParameters]]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
Auth parameters
-(NSDictionary*)authParameters
{
NSDictionary* dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:#"code",#"response_type", redirectURI,#"redirect_uri", API_ID,#"client_id", #"https://www.googleapis.com/auth/calendar",#"scope", nil];
return dictionary;
}
The response is working fine but all I get is a Google HTML login.
I checked out and google has it's own Objective-C library which is far too complicated from my point of view and is not based on iDevices but Desktop.
My 1st question is:
Is there a SIMPLE way to do it all via REST ( login, token, CRUD... ) just sending and posting requests?
e.g: http://www.googleapis.com/login?user=MY_EMAIL&password=MY_PASSWORD (<- of course not than simple / insecure but you get the idea...)
For question 1, try implementing this NSURLConnection delegate method. It allows you to supply credentials when the service asks for them.
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge