Login with Twitter¶
To enable Twitter account log in to your Backendless application:
- Create your application at https://developer.twitter.com/en/apps.
- In the application's Settings tab, set the Website to https://api.backendless.com
- Set the Callback URLs to:
https://api.backendless.com/YOUR-APP-ID/REST-API-KEY/users/social/oauth/twitter/request_url https://api.backendless.com/YOUR-APP-ID/REST-API-KEY/users/social/twitter/authorize
make sure to replaceYOUR-APP-ID
andREST-API-KEY
to the values assigned to your Backendless app. - From the Keys and tokens tab, copy the Consumer API key and the API secret key values.
- Click the Permissions tab and select the Request email addresses from users checkbox. Click Update Settings.
- Log in to your account in the Backendless Console, select your app and click Manage.
- Scroll down to the Social Settings section and insert the values copied on step 4 into the Twitter Consumer key and Twitter Consumer secret fields.
- Add Twitter Kit to your project per the instructions from: https://dev.twitter.com/twitterkit/ios/installation
- The following methods should be used in the client application to perform login with a twitter account:
- (void)loginWithTwitterWithAuthToken:(NSString * _Nonnull)authToken authTokenSecret:(NSString * _Nonnull)authTokenSecret fieldsMapping:(NSDictionary * _Nonnull)fieldsMapping responseHandler:^(BackendlessUser * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func loginWithTwitter(authToken: String, authTokenSecret: String, fieldsMapping: [String: String], responseHandler: ((BackendlessUser) -> Void)!, errorHandler: ((Fault) -> Void)!)
Add the fields mapping with Twitter to allow the users to log in to your application with their Twitter account:
TWTRLogInButton *logInButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) { if (session) { [Backendless.shared.userService loginWithTwitterWithAuthToken:session.authToken authTokenSecret:session.authTokenSecret fieldsMapping:@{@"email": @"email"} responseHandler:^(BackendlessUser *loggedInUser) { NSLog(@"User has been logged in with Twitter: %@", loggedInUser.email); } errorHandler: ^(Fault *fault) { NSLog(@"Error: %@", fault.message); }]; } else { NSLog(@"Twitter login failed: %@", [error localizedDescription]); } }]; logInButton.center = self.view.center; [self.view addSubview:logInButton];
let logInButton = TWTRLogInButton(logInCompletion: { session, error in if let session = session { Backendless.shared.userService.loginWithTwitter(authToken: session.authToken, authTokenSecret: session.authTokenSecret, fieldsMapping: ["email": "email"], responseHandler: { user in print("User has been logged in with Twitter: \(user.email)") }, errorHandler: { fault in print("Error: \(fault.message ?? "")") }) } else { print("Twitter login failed: \(error!.localizedDescription)"); } }) logInButton.center = self.view.center self.view.addSubview(logInButton)
where:
Argument | Description |
---|---|
twitterFieldsMapping |
a mapping between the Twitter fields and Backendless user properties. Keys must be the names of the Twitter fields, values - the names of the Backendless properties. |
stayLoggedIn |
a boolean value requesting user login information to be saved on the client side, so it can be reused when the application restarts (or page is reloaded). |