Verify Twitter login from iPhone on the server side - iphone

Ok, so I've currently implemented Facebook login in my app the following way:
I use the official FB framework to login the user. When I login, I get a authentication token, which is sent to my server. I then do another verification of the user (e.g. get "me" from Facebook, using the auth-token), and then return 32 char random key, which is used to identify the user in subsequent API-calls (to my server). An example.
I'm trying to figure out how to do the same with twitter, but I can't understand how to get the oath token in iOS? I have the server-side part working in another app, but no token to verify...
Please advice – is this (the FB way) how I should be doing this, or how would you go about the verification process?

Sean Cook, engineer # Twitter has a github repo with a simple app that do exactly what you are trying to do, I'using this code in my app and it works like a charm.

There's a good article at dev.twitter.com describing exactly that. Basically you'll have to first get a special request token by setting the x_auth_mode parameter to the value reverse_aut and then get the proper access token by sending what you got in the first step as x_reverse_auth_parameters.

If you are going for an iOS 5 solution, you can import this into your header file
#import < Twitter/TWTweetComposeViewController.h >
and then in the .m file where you want to authenticate
if ([TWTweetComposeViewController canSendTweet])
{
TWTweetComposeViewController* twc = [[TWTweetComposeViewController alloc] init];
[twc addImage:uiImage
[self presentModalViewController:twc animated:YES];
twc.completionHandler = ^(TWTweetComposeViewControllerResult result)
{
if (result == TWTweetComposeViewControllerResultCancelled)
NSLog(#"Tweet compostion was canceled.");
else if (result == TWTweetComposeViewControllerResultDone)
NSLog(#"Tweet composition completed.");
// Dismiss it
[self dismissModalViewControllerAnimated:YES];
};
[twc release];
} else
{
//can't tweet
}
You can also add URLs, text and other sorts of information.
Edit: You can find a tutorial on adding the needed Library to your project here,
https://dev.twitter.com/docs/ios/how-add-twitter-framework-your-ios-project

Related

Google+ integration in iPhone [duplicate]

I have been searching for tutorials on google for posting some text on google plus. But it seems like there are none.
I have also tried to go through the docs provided by google for developers of mac and iPhone but can't find anything that will solve my problem. Also there are little information on how to get the user login to there google plus account.
I am not sure what to do for the user login, do I have to use some GTLObject or use UIWebView like foursquare for user login.
Here is a list of docs that I had went through.
http://code.google.com/p/google-api-objectivec-client/wiki/Introduction
http://code.google.com/p/google-api-objectivec-client/wiki/BuildingTheLibrary
http://code.google.com/p/gtm-oauth2/
As it turns out there are only a limited number of api's available for google + developers and that also only as GET calls according to the developer of google + page my question will not get any definite answers as google is in the process of creating new api's for accessing user information on google plus.
https://developers.google.com/+/api/
Also you can use the google client sdk provided by google but it is much easier to show a webview for user login. I managed to get the people list from google plus.
The steps are same as for getting the access token as in foursquare. Just with some small changes.
in viewdidload method.
NSString *authStr = #"https://accounts.google.com/o/oauth2/auth?client_id=client_id&redirect_uri=http://somevalidurl.com&scope=https://www.googleapis.com/auth/plus.me&response_type=token";
as a url for loading request in webview. People should note one thing here that you need to create a client id in api console for your app that is web based and not as installed for this purpose as you wont we getting any option to enter any website url for callback which is very important in this case.
and in webview delegate method webViewDidFinishLoad:
NSString *urlStr = [[webView.request URL] absoluteString];
NSArray *array = [urlStr componentsSeparatedByString:#"access_token="];
if(array.count > 1)
{
NSString *oauth_token = [[[array objectAtIndex:1] componentsSeparatedByString:#"&"] objectAtIndex:0];
//do something with the oauth token after this.
}
I wrote a code snippet to signin and post simple text to Google Plus with URL and thumb Image URL.
Try it
Take a look here:
http://code.google.com/p/google-api-objectivec-client/source/browse/#svn%2Ftrunk%2FExamples%2FBooksSample
http://code.google.com/p/google-api-objectivec-client/

How to use the Facebook API, and how can I post a link with status on the users wall via my app?

I'm trying to post a link + status through my app, using this "popup" facebook window where the user is asked to log in, then asked to allow my app to his profile. Then I want him to be able to write a status, with a link I specify attached. This facebook popup would be triggered by a button, not at start of my app (in the documentation I found that they want me to implement it in the appDidFinish launching method...) But I don't want that, I want it like the popup facebook window is only triggered per button tap.
I'm trying to figure this out for a few days now, but finding that all tutorials are out-dated and the Facebook Developer documentation is very weak- atleast for for me. I already downloaded the Facebook-iOS-SDK with the API and JSON included from GitHub. I already did a few attempts, but the furthest I got is that the user was asked to log in, but not through my app, instead Safari opened and the log in field were there, not in my app.
Could someone please post some code, a basic tutorial I might find helpful, or point me to a good online tutorial? I already tried to post to iPhone Dev SDK forums, where other people also asked for this, but never got any help. So could someone out here please put together something that would help out all the newbies?
I want my login screen look like this, and the rest should be in a popup too:
http://www.facebook.com/photo.php?pid=439332&l=c04b210914&id=100001688115647
Thanks a lot in advance!
There is already a sample code in facebook-ios sdk you can see that there are already the code available for some things. Here is a code snippet from the older facebook API but this should work with the newer graph api too
- (void)setStatus:(id)target {
NSString *statusString = #"Testing iPhone Connect SDK";
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
statusString, #"status",
#"true", #"status_includes_verb",
nil];
[[FBRequest requestWithDelegate:self] call:#"facebook.users.setStatus" params:params];
}
WHat you can do is create a controller to handle all the facebook requests like a singleton class and on tap of button do something like this
- (IBAction)showFBLoginDialog:(id)sender {
if(LoggedInFacebook == NO) {
FBLoginDialog *dlg = [[[FBLoginDialog alloc] initWithSession:faceBookController._session] autorelease];
[dlg show];
}
else {
[faceBookController logoutUser];
}
}
If you need further help search on google and stackoverflow you will find a bunch of.

facebook-ios-sdk logout question

I have seen a lot of questions here regarding the Facebook Graph API but I still haven't find a solution for simple 'login'/'logout' operations using it. Looks like the Single Sign-On style is causing more confusion than benefits.
I'd like to know if it is possible have the following situation:
Enter in the app (no accessToken/expirationDate created).
Perform a login using SSO by calling authorize:delegate: method (application goes background and the login is made in the 'global' scope (Facebook App/Mobile Safari), asking for the user credentials.
Enter back in the app (now logged in, both accessToken and expirationDate are saved to NSUserDefaults).
Perform a logout by calling the logout: method (now logged out, both accessToken and expirationDate are removed from NSUserDefaults)
Attempt to perform a login again, with exactly the same steps done in 2.
I realize that when I call logout:, I do really log out from Facebook (accessToken is invalidated) from my App scope, not from the global scope (Facebook App/Mobile Safari). In 5.) when I try to log in again, the application goes to background and the login attempt is made again in Facebook App/Mobile Safari as usual, however I'm getting a screen saying that I'm already logged in:
You have already authorized .... Press "Okay" to continue.
Logged in as ... (Not You?).
It's a strange behavior for the user that has just logged out in my App.
My question is:
"Can I really log out from facebook (I mean 'global' scope) from inside my App? This would affect other apps using the facebook credentials too. However, if I can't to do this, how can I avoid the 'strange behavior' describe above?
Thanks
Eduardo,
I feel your pain! I spent the better part of a day working on this issue. I have discovered that when you use SSO and the call:
Called from your code:
[facebook logout:self];
Facebook API method:
- (void)logout:(id<FBSessionDelegate>)delegate {
self.sessionDelegate = delegate;
[_accessToken release];
_accessToken = nil;
[_expirationDate release];
_expirationDate = nil;
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:[NSURL URLWithString:#"http://login.facebook.com"]];
for (NSHTTPCookie* cookie in facebookCookies) {
[cookies deleteCookie:cookie];
}
if ([self.sessionDelegate respondsToSelector:#selector(fbDidLogout)]) {
[_sessionDelegate fbDidLogout];
}
}
The facebook API does invalidate the access token and expirationdate variables and attempts to delete the mobile Safari cookies, but for some reason, probably Apple's fault the cookies are not really deleted. So when you attempt to login in the next time your mobile Safari will see the cookie and it says:
"You have already authorized .... Press "Okay" to continue. Logged in as ... (Not You?)."
Until either Facebook finds a fix or Apple fixes their broken API we must bypass SSO through Safari. Below are the changes I made to Facebook.m in order to force the old login dialog. If you used these changes they may not work forever but it is my guess that they will work for a very long time. Also to be sure this worked with the most recent facebook API I updated to the latest as of this post (Nov 2011 build).
Called from your code:
[facebook authorize:permissions];
Facebook API method:
- (void)authorize:(NSArray *)permissions {
self.permissions = permissions;
// [self authorizeWithFBAppAuth:YES safariAuth:YES];
[self authorizeWithFBAppAuth:NO safariAuth:NO];
}
If this helps you please up rate this thread and my post to help others find it.
gadildafissh
I'm afraid the answer is no, you can't do this.
Your application is in a sandbox, and can't write outside, where global cookies are (for mobile safari) and Facebook app settings (in Facebook app preferences/cookies I think)
You can only warn your user to logout outside of your app...
...Or you can just not use facebook api SSO, but in app login webform, like I do for other reasons.
If you choose that option this pull request might save you some time ;)
Hii ,
its not possible , the reason is for Single Sign On (SSO) is not to make user login everytime, he logouts , instead if the user logs in anyone of FB enabled apps - it will use that to login again - This is because the device is mostly used by single person in this case only one user can login in Facebook.
you can't control any app outside of your app - for Example - if u login with Gmail & when you open google.com you can see your username there is currently logged In which has SSO,
In new SDK of Facebook, you can set login button loginBehaviour property
Below code in swift ...
let fbButton = FBSDKLoginButton()
fbButton.loginBehavior = .Web
Answer already done, but I just want to clarify it. May be it saved somebody's time.
Go to Facebook.m and change line
[self authorizeWithFBAppAuth:YES safariAuth:YES];
to
[self authorizeWithFBAppAuth:YES safariAuth:NO];
It will cause login window appear inside the app. Logout will work perfect. In other words, it will work as it used to in older versions of OS.
in addition to kishnan94 answer. the objective c version is ;
if you want a modal to open up and ask for facebook credentials seperately from Safari or Facebook app, just use the latest facebook sdk and set the login behaviour
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login setLoginBehavior:FBSDKLoginBehaviorWeb];
this will make the logout process more convenient and less confusing for users without using safari or facebook app accounts.
It seems that this is a bug of Facebook SDK. In a case of the Facebook app is installed on device, access_token is renewed. In other hand, access_token and expirationDate could not be changed. :((

How to send direct message with Twitter?

in my iphone application i have to send direct message throughout twitter,
the problem is that twitter had changed the authentication from basic authentication to oauth
and tutorial on the Web are out of date
so i use SAOauthTwitterEngine for the authentication part and all ok. but these classes don't use the api, so i have to use MGTwitterEngine for this.
the problem is that MGTE dont have oauth but only xauth and basic authentication, and i cant use this together anyone know how to do it? or know a tutorial that explain it.
thanks a lot.
i suggest you OAuth because xAuth requires you to send an email to twitter askeing them to give you the permission, and you have to write a reort on your application (this isn't good for a testing app)
have you tried this?
https://github.com/bengottlieb/Twitter-OAuth-iPhone
in my app I am sending direct messages to my twitter followers by using MGTwitterEngine let have a look:
appDelegate._engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self];
appDelegate._engine.consumerKey = kOAuthConsumerKey;
appDelegate._engine.consumerSecret = kOAuthConsumerSecret;
once you get authenticated this method get called
#pragma mark SA_OAuthTwitterControllerDelegate
- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *)username {
NSLog(#"Authenicated for %#", username);
[appDelegate._engine sendDirectMessage:#"Test for Twitter direct message" to:username];
}
Hope this will help you.

Facebook Connect on iPhone question

When you use facebook connect on the iPhone do you have to use the supplied login button and login screen built into the framework? The reason I ask is because I'm also using twitter and I would like to have the same user experience when they log in to user as they have when they log in to facebook. So I can either replicate the login screen facebook connect uses for twitter or just not use the facebook connect login screen all together.
Login button: no. Login screen: yes. I added FB Connect integration to my FriendFeed app for iPhone, Stir, and skipped the login button. Instead, a user can choose a "Share on Facebook" button on a UIActionSheet and the app either displays a login screen or automatically posts a link depending on whether the user is authenticated.
Here's a code snippet for you. If the session is successfully resumed, then a method on your FBSession object's delegate will be called.
if (![fbSession resume]) {
FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:fbSession] autorelease];
[dialog show];
}
- (void)session:(FBSession*)session didLogin:(FBUID)uid {
NSLog(#"Hooray, I'm logged in to Facebook!");
}
Apologies for being a little vague in my example above. To be honest, I find FBConnect to be a bit of a mystery and tried my best to implement it and get away from it as quickly as possible in my app. Let me know if you need more information and I'll put together a more-concrete answer.
Per the request below:
You can get an FBSession object with FBSession's +sessionForApplication:secret:delegate class method. You call -resume on the session object. If it returns YES, it'll immediately call your delegate's -session:didLogin: method, which is where you should put your FB-dependent actions. If it does not successfully -resume, then you need to create an FBLoginDialog, as seen in the code snippet above. Make sense? Let me know if you need more info