Here, we will explain the method of single sign-on (SSO) using OAuth on Twitter.
For single sign-on, use the InAppBrowser plug-in and a third-party advanced-http plug-in, and the signature required for OAuth uses the oauth-signature library.
After successful authentication, you can display the user's basic information on the app and post tweets.
If you want to use third-party Cordova plugins, you need to create a custom build debugger (Android or iOS).
Create Standalone Apps from the Create App button at the bottom of the screen. (Instead of creating Standalone Apps, you can create Apps under Projects)
Enter the Name (app name), Description (app description), and Website (URL from which the app will be downloaded). (* You cannot use a name that has already been used. There is a limit to the number of apps that can be created per day.)
Enter the Callback URL (optional: the page that will be displayed after successful authentication). In this sample app, set mymonacaapp://. Please change to the one for your app later. This Callback URL is also required when implementing the app.
Select the Settings tab and from App permissions, grant Read and Write permissions. If you do not tweet from the app, only allow Read permission.
Select the Settings tab and from Authentication, enable 3-legged OAuth.
Import plugin
We will use the InAppBrowser plugin and a third-party advanced-http plugin for single sign-on and use the oauth-signature library to create the signatures needed for OAuth.
From the Monaca Cloud IDE, select Settings → Manage Cordova Plugins.
Enable the InAppBrowser plugin.
3. Click the button "Import Cordova Plugin", check "Specify URL or package name", enter cordova-plugin-advanced-http for the package name, and click the OK button.
Search by typing oauth-signature-js in the component search form. Install oauth-signature-js from the search results. After that, select components/oauth-signature-js/dist/oauth-signature.js in the selection of the load target and save it. Please note that the oauth-signature.js library is BSD-3-Clause, so please be careful when distributing the source code and binary code of the application.
App description
File organization
File
Description
index.html
App screen page
css/style.css
App stylesheet
js/app.js
JavaScript file that performs various processing when the application is executed
In the first three lines of the file, set the API key (consumer key), API key secret (consumer secret), and callback URL you obtained from the Twitter Developer account.
Tap the Connect button to display the authentication screen.
After a successful login, the access token and user ID are stored in the variable model. In this sample app, there is no log-out function, so you need to kill the app from the task to cancel the login status.
Then tap the Show Me button to see the logged-in user information and the icon. Furthermore, when you tap the Send Tweet button, the message entered in the text form will be posted to Twitter.
If you check "Remember me" on the Twitter login screen, the authorization screen will be displayed in the logged-in state without entering the account and password from the next time. If you want to log out from Twitter, please sign out from the icon on the upper right of the authorization screen.
In this app, the login information (access token) is temporarily stored in the variable model, so the login information will be lost when the app is restarted. If you want to stay logged in, save your access token and user ID in a persistent location such as localstorage.