Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Log In with Twitter

Eric Frohnhoefer edited this page Dec 7, 2017 · 6 revisions

TwitterCore provides Log In with Twitter. This feature enables application users to authenticate with Twitter. Before using this feature, ensure that "Sign in with Twitter" is enabled for your Twitter app (https://apps.twitter.com).

When attempting to obtain an authentication token, TwitterCore will use the locally installed Twitter app to offer a single sign-on experience. If TwitterCore is unable to access the authentication token through the Twitter app, it falls back to using a web view to finish the OAuth process.

The simplest way to authenticate a user is using TwitterLoginButton. Inside your layout, add a Login button with the following code:

This will render a button that looks like:

image

Log In Button

In the Activity or Fragment that displays the button, you need to create and attach a Callback to the Login Button.

Pass the Activity's Result Back to the Button

Next, pass the result of the authentication Activity back to the button:

If using the TwitterLoginButton in a Fragment, use the following steps instead. Inside the Activity hosting the Fragment, pass the result from the Activity to the Fragment.

TwitterSession

If login completes successfully, a TwitterSession is provided in the success result. This TwitterSession will contain a token, secret, username, and user ID of the user and becomes the active session and is automatically persisted. If you need to retrieve the TwitterSession at a later time, you may do so using the SessionManager.

Request User Email Address

Before using this feature, ensure that "Request email addresses from users" is checked for your Twitter app (https://apps.twitter.com). To request a user's email, call the TwitterAuthClient#requestEmail method, passing in a valid TwitterSession and Callback.

If the email address is available, the success method is called with the email address in the result. It is not guaranteed you will get an email address. For example, if someone signed up for Twitter with a phone number instead of an email address, the email field may be empty. When this happens, the failure method will be called because there is no email address available.

Guest Authentication

Twitter supports two authentication types for a logged out Twitter experience. Application authentication allows an app to issue API requests on its behalf instead of the user’s; it is limited to requests that do not require a user context. This means, for example, that you cannot Tweet with application authentication, but you can get a user's last Tweet. See Access Twitter’s REST API. Guest authentication is an extension to application authentication, but there are two major differences:

1. Rate Limits

When using application authentication, rate limits are determined globally for the entire app. For example, an app can make 300 requests every 15 minutes against the GET statuses/user_timeline endpoint. This means that if your mobile app is being concurrently used by 100 users – those 300 requests are shared by 100 users. On the other hand, guest authentication rate limits scale – each of those 100 users will now be allocated 180 requests every 15 minutes.

Internally, Twitter Kit calls a number of endpoints, notably:

These endpoints are rate limited to 180 requests per 15 minutes under guest authentication, the exception being GET collection/entries which is limited to 1000 requests per 15 minutes.

2. Token Expiration

User tokens do not expire, but those generated by guest authentication do. However, Twitter Kit will automatically handle guest token refresh.