I am trying to fetch the number of times a link is shared on google plus in my iOS app. I am trying the following code
NSString *post = #"[{\"method\":\"pos.plusones.get\",\"id\":\"p\",\"params\":{\"nolog\":true,\"id\":\"http://stylehatch.co/\",\"source\":\"widget\",\"userId\":\"#viewer\",\"groupId\":\"#self\"},\"jsonrpc\":\"2.0\",\"key\":\"p\",\"apiVersion\":\"v1\"}]";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat : #"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"https://clients6.google.com/rpc?key=my_api_key"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *requestResponse;
NSError *err;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:&err];
return data;
but it returns the following error
"Access Not Configured. The API (+1 API) is not enabled for your
project. Please use the Google Developers Console to update your
configuration."
In Google Developers Console
https://console.developers.google.com/project/googeplussignin/apiui/apis/library
Select your project
2.Left Pane : Select Apis & Auth
There you can find so many API. Enable needed API.
It seems google no more allows the use of this api for public use. I found the answer here.
We now need to send a Get request to https://plusone.google.com/_/+1/fastbutton?url=http://www.facebook.com
Related
I need to make a HTTPS post request to a server using a synchronous connection. I've made plenty of http connections but for some reason the code doesn't work with https and I'm sure the website address is right.
Some of my code:
Note that dparam is a dictionary object.
NSError *err;
NSData *parameterJSONData = [NSJSONSerialization dataWithJSONObject:dparam options:
NSJSONWritingPrettyPrinted error:&err];
NSString *param = [[NSString alloc] initWithData:parameterJSONData encoding:NSUTF8StringEncoding];
NSString *post =[[NSString alloc] initWithFormat:#"varnameOne=%#&varnameTwo=%#¶m=%#",vone,vtwo,param];
//my_url is a string defined elsewhere
NSURL *url=[NSURL URLWithString:my_url];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
I need to do this synchronously as I don't really have the time to change all my database calls to asynchronous and the calls need to either complete or fail before the program continues so it'd have to be made effectively synchronous anyway.
That said I did try a synchronous route but it didn't work with https only http.
This question already has answers here:
How to use NSURLConnection to connect with SSL for an untrusted cert?
(13 answers)
Closed 1 year ago.
I'm using the following code to send a user login to a server, but Apple says that I'm using a private API and will reject the app. How can I resolve this?
would replacing with [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; [request setHTTPMethod:#"POST"];
be enough?
#interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
#end
and my login code:
// token url
NSURL *url=[NSURL URLWithString:#"http://xxxxxxxx.com/api/token"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSLog(#"url is %#",url),
NSLog(#"request is %#",request),
**[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];**
There seems to be a public API for that.
How to use NSURLConnection to connect with SSL for an untrusted cert?
Must be late but might help.
I have an iOS application which connects via OAuth 2.0 to Facebook. I would like to make a POST request which achieves the equivalent of this code in iOS:
curl -F 'access_token=...' \
-F 'message=Hello. I like this new API.' \
https://graph.facebook.com/[USER_ID]/feed
I found a nice tutorial online and I followed all the instructions, but I still can get my POST request to work. Here is my code:
NSString *post = post_text.text;
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *ready_post = [NSString stringWithFormat:#"https://graph.facebook.com/1313573269/feed?message=%#&access_token=CAACEdEose0cBAHgdZAon2EZBZCzjoLkhg7jrqvZAbliuoOQ2E2Exc4rZCAxPzeVEADwnQaNLYuG16Gq6q6LLLLLLLLVQ7LZAncXCc53qE2iyzleZAPXGajsgjnBTuo6YdJCZAxVGIYYD8sZCgQ9ypZCo0iOZBNuyrechBfee2yptlK9tmgdosZA7wrKg8ZD", postData];
[request setURL:[NSURL URLWithString:ready_post]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
As you can see I am hard coding the access token for the time being just as a test. But I cant seem to get my app to actually POST anything to Facebook.
Thanks, Dan.
You're trying to post a JSON to the GraphAPI - which you can't do.
Instead of that, you have to use parameters when posting (query string or HTTP request parameters).
See: Publish to Feed.
I am trying to edit the wikitext of a page using a textView and save it on the server using mediawiki API as follows:
- (void)saveAction{
NSString *savedString = textView.text;
NSString *baseurl=[[NSUserDefaults standardUserDefaults] stringForKey:#"url_preference"];
NSString *page=[[baseurl stringByAppendingString:#"/api.php?**action=edit&title=Testedit&text=savedString&token=**"] stringByAppendingString:[MySingleton sharedSingleton].token];
NSData *data=[savedString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postlength=[NSString stringWithFormat:#"%d",[data length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:page]];
[request setHTTPMethod:#"POST"];
[request setValue:postlength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:data];
NSError *error=nil;
NSURLResponse *response=nil;
NSData *result=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *HTMLString2 = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSLog(#"%#", HTMLString2);
}
but I am getting an error : internal_api_error_MWException,
Exception Caught: Detected bug in an extension! Hook iaifAPIEditBeforeSave has invalid call signature; Parameter 1 to iaifAPIEditBeforeSave() expected to be a reference.
Searched about the error on google but I didn't find anything. Please suggest something.
That wiki appears to have an outdated Data Import Extension. Update/uninstall as required. A quick hack that should fix this particular error would be to replace function iaifAPIEditBeforeSave(&$editPage, $text, &$resultArr) with function iaifAPIEditBeforeSave($editPage, $text, &$resultArr) in extensions/DataImport/IAI/includes/IAI_GlobalFunctions.php, however I don't know what else could be outdated/broken there.
I've successfully implemented push notifications with Urban Airship in my app, but I want to make an administrative password-protected part of the app that enables one to send the push notifications from any authorized iPhone with the app installed.
I need a way so whatever i type in a text box is send via https POST to https://go.urbanairship.com/api/push/broadcast.
Can anyone recommend or tell me a way to do this?
You can use NSURLConnection to do this. Setup an NSMutableURLRequest with the post data and other information, and then use NSURLConnection to post it and get a response:
NSString * postString = [NSString stringWithFormat:#"field1=%#", myItem];
NSData * postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
NSURL * myURL = [NSURL URLWithString:#"https://go.urbanairship.com/api/push/broadcast"];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [postData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:postData];
NSData * returnedData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
You can also use NSURLConnection for asynchronous requests. See the NSURLConnection Class Reference.