Am learning about iOS programming from book Head First iPhone Programming. In one exercise, they have sample code for Twitter which uses basic authorization. Now that Twitter uses OAuth, how can I get OAuth code so that I can test my client? Do I need to register my app with Twitter? How do I do that, since it's only a test app?
Here is the basic authorization version of the code; I'm looking for the OAuth version:
//TWITTER BLACK MAGIC
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://YOUR_TWITTER_USERNAME:YOUR_TWITTER_PASSWORD#twitter.com/statuses/update.xml"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody:[[NSString stringWithFormat:#"status=%#", themessage] dataUsingEncoding:NSASCIIStringEncoding]];
NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSLog(#"%#", [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]);
//END TWITTER BLACK MAGIC
To get Twitter oAuth access you will need to create app on http://dev.twitter.com and retrieve Consumer Key and Consumer Auth.
Perhaps this tutorial will help you:
http://mobile.tutsplus.com/tutorials/iphone/twitter-api-iphone/
Related
We're using a NSURLSessionDataDelegate and have the following delegate method implemented:
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler { ... }
Frequently we find that users of iCloud receive a NSURLAuthenticationMethodXMobileMeAuthToken challenge. Sending back a rejection doesn't work (as the server won't try another challenge) and sending back an acceptance with
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
doesn't work either.
What are we supposed to do with this authentication challenge type? Has anyone else seen it?
For any protection space that you aren't explicitly handling, or for any credential type that you don't understand, you should typically use NSURLSessionAuthChallengePerformDefaultHandling (the credential is ignored, but typically pass nil) to tell the OS to handle the credential as though you didn't provide an authentication delegate method.
I am developing an app for iOS. The app is to store the tweetIDs of the tweets the user has made which include another twitter handle in.
I can successfully log a user into a twitter account on the device through the Social Framework and call the API to receive and page through past tweets. However this takes up a lot of bandwidth on the device & I would prefer for the server (Parse.com) to do this for me, as it is here where the IDs will be stored. Is the social framework enough for this or do I need to create a Oauth from scratch for Java SDK for coding the parse server?
Thanks
D
NSString * requestString = [NSString stringWithFormat:#"https://api.twitter.com/1.1/users/show.json?screen_name=%#", user.username];
NSURL *verify = [NSURL URLWithString:requestString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:verify];
[[PFTwitterUtils twitter] signRequest:request];
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
You pass in the username of the user you want to authenticate for. Then create an NSDictionary object and use the 'NSJSONSerialization JSONObjectWithData:options: method with the NSData data object that you get from the request to be able to use the results of the response. Happy coding.
Hi I have an issue here with regards to sending Push Notifications from my app. I can send them from the admin panel and they are received by the device but not from device to device. I have re-uploaded both development and production certificates on a couple of occasions now. Also push notifications can only be sent from the admin panel in the sandbox environment (no errors) and not the production environment (No recipients. At least one user should be subscribed for APNS (Apple Push) (through SDK or REST API). Can someone help please?
//
NSString *message = #"Testing APNS!";
NSMutableDictionary *payload = [NSMutableDictionary dictionary];
NSMutableDictionary *aps = [NSMutableDictionary dictionary];
[aps setObject:#"default" forKey:QBMPushMessageSoundKey];
[aps setObject:message forKey:QBMPushMessageAlertKey];
[payload setObject:aps forKey:QBMPushMessageApsKey];
QBMPushMessage *pushMessage = [[QBMPushMessage alloc] initWithPayload:payload];
// Send push to user
[QBMessages TSendPush:pushMessage toUsers:self.opponent.login delegate:self];
//
[QBMessages TSendPush:pushMessage toUsers:self.opponent.login delegate:self];
You can's send push notification to user's login, you have to use user's ID
For example:
[QBMessages TSendPush:pushMessage toUsers:#"22,33,77" delegate:self];
I'm using MGTwitterEngine and OAuthConsumer frameworks. And mostly following the instructions at UsingOAuthConsumer.
In order to use OAuth and not have the user deal with the oob PIN based authentication, you need to enable a callback to the application. To do this on a desktop (or iOS) application, you need to set up a custom URI scheme that goes to an event handler in the app. I got this working, and tested it by using the custom URI in Safari. My app does open and the correct method is invoked. So far so good.
To do this for Twitter, you need to specify the callback URI in the settings for the application on Twitter's dev site. Here the problem starts. Twitter won't allow non-standard URIs. So "myapp://oauth/" is not allowed. It has to be an http or https URI. All the websites I referenced say to put a placeholder here, and override in the request token request. OK, so I put a dummy URL for my website here. Now to implement the override. Here's the code from one of the comments on how to so that:
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:consumer
token:nil
realm:nil
signatureProvider:nil];
[request setOAuthParameterName:#"oauth_callback" withValue:#"callbackurl:"];
When I add that second method call, the request to twitter now fails. NSURLErrorDomain error -1012 or something similar (I forgot to write down the number).
I tried a number of ways, but was never able to override the callback URL. Does anyone have a sure-fire way of doing this? For now, I've changed the app to use the OOB PIN authentication method, but I'd sure like to remove that unnecessary step for the user.
Thanks!
joe
I finally gave up on the OAuthConsumer framework and switched to the Google GTMOAuth framework. That works fine.
How to integrate Twitter support into an iOS application?
Stefan Arentz's Twitter library is a pleasure to use - it contains the views you need, there is basically no coding to do. Do register your app early with twitter and get a submission to request xAuth authentication permission so that you do not have to use the clumsy web-based oAuth method. With xAuth it is as simple as submitting username and password and keeping the returned authentication token to submit a tweet.
Here is an example:
Call -(void)sendTweet from your IBAction.
// Sending tweets from within the application
-(void)sendTweet {
// check if device capable of sending tweets
if([TWTweetComposeViewControllercanSendTweet])
{
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewControlleralloc] init];
[tweetSheet setInitialText:#"Twitting from my iSecret App"];
self.imageString = #"theSecret.png";
if(self.imageString) {
[tweetSheet addImage:[UIImageimageNamed:self.imageString]];
}
[selfpresentModalViewController:tweetSheet animated:YES];
}
else {
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:#"Sorry"message:#"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"delegate:selfcancelButtonTitle:#"Ok"otherButtonTitles:nil];
[alert show];
}
}