Monaca Docs
  • Monaca
  • Create Your First App
  • Release Notes and Updates
    • Privacy Manifest Requirement for iOS Apps
    • Plugin uses-permission Tag Deduplication Feature
    • iOS Monaca Debugger Discontinuation & Alternative Features
    • Error submitting to iOS App Store (ITMS-90165)
    • Build error in cordova-custom-config
    • Cordova 11 changes
    • Cordova 10 changes
    • Cordova 9 changes
    • New Monaca CLI and Localkit
    • Migration from Cordova to Capacitor
  • Product Guides
    • Monaca Development Overview
    • Monaca Cloud IDE
      • Overview
      • Features in the Monaca Cloud IDE
      • Integrated Terminal
      • Editor Shortcuts
      • Project Dependencies
        • File and Folder Structure
        • JS/CSS Components
        • Cordova Plugins
        • Custom Cordova Plugins
      • Version Control
        • Introduction
        • GitHub Integration
        • Git SSH Integration
      • Monaca CI
        • Overview
        • Deploy Services
        • Deploy to Appetize.io
        • Deploy to DeployGate
        • Deploy to Firebase
      • Build
        • Building for iOS
          • Building an iOS App
          • Build Settings between Monaca and Xcode
        • Building for Android
        • Building for Electron
          • Building on Windows
        • Building for PWA
        • Building for Windows
        • Build Environment Settings
        • Common Build and Application Upload Errors
        • Build History
      • Distribution
        • App Store Distribution
          • App Store Connect Guide
          • iOS App Upload Feature
        • Google Play Distribution
        • Amazon Appstore Distribution
        • Non-market App Distribution
      • Download App Package
      • Tutorial
    • Monaca Localkit
      • Overview
      • Pairing and Debugging
      • Remote Building and Publishing
      • Troubleshooting Guide
      • Tutorial
    • Monaca CLI
      • Overview
      • Monaca CLI Commands
      • Pairing and Debugging
      • Project Dependencies
        • File and Folder Structure
        • JS/JSS Components
        • Cordova Plugins
        • Custom Cordova Plugins
      • Remote Building and Publishing
      • Troubleshooting Guide
      • Tutorial
    • Monaca Debugger
      • Functionalities
      • Installation
        • Monaca Debugger for Android
        • Monaca Debugger for iOS
        • Monaca Debugger for Android Emulator
      • Usage
      • Custom Build Debugger for iOS
      • iOS App Simulator Build
      • Troubleshooting Guide
      • Tutorials
    • Team Dashboard
    • Quick Viewer
    • Migrating from Other Platforms
      • Key Points
      • Cloud IDE preview function settings
      • Migrating from Angular
      • Migrating from Ionic
      • Migrating from React
      • Migrating from Vue
      • Migrating from PhoneGap
        • Key Differences
        • Guide for PhoneGap Build Users
        • Guide for PhoneGap CLI Users
        • Guide for PhoneGap Desktop App Users
      • Migrating from Telerik
  • Build App
    • Build for iOS
      • Creating a Private Key and CSR
      • Creating a Certificate
      • Updating Provisioning Profiles
  • Tutorials
    • Monaca Cloud IDE Tutorial
      • Part 1: Starting a Project
      • Part 2: Running Monaca Debugger with Monaca Cloud IDE
      • Part 3: Building a Monaca App
      • Part 4: Publishing a Monaca App
    • Monaca Localkit Tutorial
      • Part 1: Starting a Project
      • Part 2: Running Monaca Debugger with Monaca Localkit
      • Part 3: Building a Monaca App
      • Part 4: Publishing a Monaca App
    • Monaca CLI Tutorial
      • Part 1: Starting a Project
      • Part 2: Running Monaca Debugger with Monaca CLI
      • Part 3: Building a Monaca App
      • Part 4: Publishing a Monaca App
    • Electron Tutorial
      • How to Use a NPM Package
      • How to Use a Web API
    • Barcode Scanner Plugin
    • Cordova SQLite Storage Plugin
    • Cordova Google Analytics Plugin
    • Cordova Firebase Plugin
    • Cordova In-app Purchase Plugin
    • Cordova AppVersion Plugin
    • Cordova Ionic Keyboard Plugin
    • Cordova Social Sharing Plugin
    • NIFCLOUD mobile backend
    • Phonegap Push Plugin
  • API Reference
    • Monaca API
      • Monaca Cloud & Remote Build API Guide
      • Utilities
    • Core Cordova Plugins
      • Cordova 11.0
        • Battery Status Plugin
        • Camera Plugin
        • Device Plugin
        • Device Motion Plugin
        • Device Orientation Plugin
        • Dialogs Plugin
        • File Plugin
        • Geolocation Plugin
        • InAppBrowser Plugin
        • Media Plugin
        • Media Capture Plugin
        • Network Information Plugin
        • Splashscreen Plugin
        • Vibration Plugin
        • StatusBar Plugin
      • Cordova 10.0
        • Battery Status Plugin
        • Camera Plugin
        • Device Plugin
        • Device Motion Plugin
        • Device Orientation Plugin
        • Dialogs Plugin
        • File Plugin
        • Geolocation Plugin
        • InAppBrowser Plugin
        • Media Plugin
        • Media Capture Plugin
        • Network Information Plugin
        • Splashscreen Plugin
        • Vibration Plugin
        • StatusBar Plugin
        • Whitelist Plugin (Android Only)
      • Cordova 9.0
        • Battery Status Plugin
        • Camera Plugin
        • Contacts Plugin
        • Device Plugin
        • Device Motion Plugin
        • Device Orientation Plugin
        • Dialogs Plugin
        • File Plugin
        • File Transfer Plugin
        • Geolocation Plugin
        • Globalization Plugin
        • InAppBrowser Plugin
        • Media Plugin
        • Media Capture Plugin
        • Network Information Plugin
        • Splashscreen Plugin
        • Vibration Plugin
        • StatusBar Plugin
        • Whitelist Plugin (Android Only)
    • Third-party Cordova Plugins
      • Advanced HTTP Plugin
      • PhoneGap BarcodeScanner Plugin
      • Cordova Custom Config Plugin
      • DatePicker Plugin
      • Share Plugin (Android)
      • WebIntent Plugin (Android)
    • Monaca Power Plugins
      • Monaca HTML5 Resource Encryption
      • Monaca In-App Updater
      • Monaca Secure Storage
      • Barcode Scanner Plugin
      • Android build memory size setting
    • Service Integration
      • Repro
      • AppsFlyer
    • Configuration Files
      • Android
        • Android Configuration
        • config.xml
        • AndroidManifest.xml
      • iOS
        • iOS Configuration
        • config.xml
        • MonacaApp-Info.plist
  • Samples & Tips
    • Sample Apps
      • AdMob
      • Twitter Single Sign-on App
      • Facebook Single Sign-on App
      • Flickr
      • TODO App
      • BirthYear App
      • Break the Bricks
      • Train Catalog App
      • Omikuji Fortune Telling App
      • Clock App
      • Memo Application
      • RSS Reader App
      • Hello World App
    • Tips & Tricks
      • Playing Sound and Music
      • Control the Splash Screen
      • Using Database
  • Features
    • Push Notification
    • SNS Authentication
    • Database
  • FAQ
    • General
    • IDE
    • Build
    • Release
    • Subscription
      • How to Use Activation Code
    • Application
    • Usage
    • Debugger
  • Supported Environments
  • Trouble Shooting
    • Preview Log repeats to reload (Vue packages version mismatch error)
  • Monaca Product Website
  • 日本語
Powered by GitBook
On this page
  • Demo
  • Prerequisite
  • Getting Twitter Consumer Key and Consumer Secret
  • Import plugin
  • Import JS components
  • App description
  • File organization
  • HTML description
  • JavaScript description

Was this helpful?

  1. Samples & Tips
  2. Sample Apps

Twitter Single Sign-on App

PreviousAdMobNextFacebook Single Sign-on App

Last updated 3 years ago

Was this helpful?

Here, we will explain the method of single sign-on (SSO) using OAuth on Twitter.

For single sign-on, use the plug-in and a third-party plug-in, and the signature required for OAuth uses the 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).

Demo

Tested Environment

  • Android 11.0

  • iOS 14.3

Prerequisite

Getting Twitter Consumer Key and Consumer Secret

Next, on the Twitter Developer Portal page, register your app and issue a Consumer Key (API key), Consumer Secret (API secret key), and Bearer token.

  1. 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)

  2. 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.)

  3. 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.

  4. 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.

  5. Select the Settings tab and from Authentication, enable 3-legged OAuth.

Import plugin

  1. From the Monaca Cloud IDE, select Settings → Manage Cordova Plugins.

  2. 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.

Import JS components

  1. From Monaca Cloud IDE, select Settings → Add / Remove JS / CSS Components.

  2. 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

HTML description

index.html

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta http-equiv="Content-Security-Policy" content="default-src * data: gap: content: https://ssl.gstatic.com; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">
    <script src="components/loader.js"></script>
    <link rel="stylesheet" href="components/loader.css">
    <link rel="stylesheet" href="css/style.css">
    <script src="js/app.js"></script>

</head>
<body>
    <h2>Twitter Sample</h2>
    <div>
        <button onclick="connect()">Connect !</button>
        <p>Your Id:
            <span id="tw-id"></span>
        </p>
        <p>Your Name:
            <span id="tw-name"></span>
        </p>
        <hr>
        <button id="showMe" onclick="showMe()">Show Me</button>
        <p>Your Info:
            <span id="tw-profile"></span>
        </p>
        <img id="tw-profile-image" class="profile">
        <hr>
        <p>
        <textarea id="tweetText" rows=5 cols=40>Hello</textarea>
        </p>
        <button id="tweetBtn" onclick="sendTweet()">Send Tweet</button>
    </div>
</body>
</html>

This page is the application screen.

This page is roughly divided into 3 blocks. Each block is separated by <hr> tags

  1. Login block:

    There is a button to move to the Twitter login screen and a component that displays the user ID and user name (screen name) after login.

  2. Profile block:

    There is a button to get the profile of the logged-in user and a component to display it after a successful acquisition.

  3. Tweet block:

    There is a text area and a button to tweet.

JavaScript description

app.js

  const apiKey = '[Consumer Key(API key)]';
  const secretKey = '[Consumer Secret (API key secret)]'; 
  const callbackURL = '[Registered callback URL (scheme)]';

  const signatureMethod = "HMAC-SHA1";
  const version = "1.0";

  const requestTokenURL = "https://api.twitter.com/oauth/request_token";
  const loginURL = "https://api.twitter.com/oauth/authorize"
  const accessTokenURL = "https://api.twitter.com/oauth/access_token";
  const updateURL = "https://api.twitter.com/1.1/statuses/update.json";
  const usersShowURL = "https://api.twitter.com/1.1/users/show.json";

  let model = {};

  function percentEncode(str) {
      return encodeURIComponent(str).replace(/[!'()*]/g, char => '%' + char.charCodeAt().toString(16));
  }

  function getNonce() {
      const array = new Uint8Array(32);
      window.crypto.getRandomValues(array);
      return Array.from(array).map(uint => uint.toString(16).padStart(2, '0')).join('');
  }

  function oauthSend(url, method, accessTokenSecret, data, oauth_params, cb) {
      const timestamp = (Math.floor(Date.now() / 1000)).toString(10);
      const parameters = Object.assign(
          {},
          oauth_params,
          {
              oauth_nonce: getNonce(),
              oauth_signature_method: signatureMethod,
              oauth_timestamp: timestamp,
              oauth_version: version
          },
          data
      );
      const signature = oauthSignature.generate(method.toUpperCase(),
          url,
          parameters,
          secretKey,
          accessTokenSecret
      );
      const authorizationHeader = "OAuth " + Object.keys(parameters).map((key) => {
          return percentEncode(key) + '="' + percentEncode(parameters[key]) + '", '
      }).join('') + 'oauth_signature=\"' + signature + '\"';

      const urlWithParams = method.toUpperCase() === "GET" ?
          url + "?" + Object.keys(data).map(function(k) {
              return encodeURIComponent(k) + '=' + encodeURIComponent(data[k])
          }).join('&') : url;

      cordova.plugin.http.sendRequest(urlWithParams, {
          method: method,
          headers: {'Authorization': authorizationHeader},
          data: data,
          serializer: null
      }, (res) => {
          cb(res);
      }, (error) => {
          alert(error.error);
      });
  }

  function connect() {
      oauthSend(requestTokenURL, 'post', "", {}, { 
          oauth_callback: callbackURL,
          oauth_consumer_key: apiKey,
      }, function (res) {
          openLoginDialog(res.data);
      });
  }

  function openLoginDialog(res) {
      const oauth = res.split('&')[0];
      const url = loginURL + "?" + oauth;
      const ref = cordova.InAppBrowser.open(url, "_blank", 'location=yes,beforeload=get');
      ref.addEventListener('beforeload', beforeLoad(ref));
  }

  function beforeLoad(ref) {
      return function (event, cb) {
          if (event.url && event.url.startsWith(callbackURL) ) {
              const url = new URL(event.url);
              const params = Array.from(url.searchParams.entries()).reduce(
                  function (acc, cur) {
                      acc[cur[0]] = cur[1];
                      return acc;
                  } , {} 
              );
              ref.close();
              getAccessToken(params);
          } else {
              cb(event.url);
          }
      };
  }

  function getAccessToken(params) {
      oauthSend(accessTokenURL, 'post', "", {}, { 
          oauth_verifier: params.oauth_verifier,
          oauth_token: params.oauth_token,
          oauth_consumer_key: apiKey,
      }, function (res) {
          const params = Array.from(new URLSearchParams(res.data).entries()).reduce(
              function (acc, cur) {
                  acc[cur[0]] = cur[1];
                  return acc;
              } , {}
          );
          document.getElementById('tw-id').innerHTML = params.user_id;
          document.getElementById('tw-name').innerHTML = params.screen_name;
          model.oauth_token = params.oauth_token;
          model.oauth_token_secret = params.oauth_token_secret;
          model.user_id = params.user_id;
      });
  }

  function sendTweet() {
      const text = document.querySelector("#tweetText").value;
      oauthSend(updateURL, 'post', model.oauth_token_secret, { "status": text }, {
          oauth_token: model.oauth_token,
          oauth_consumer_key: apiKey
      }, function (res) {
          alert("Tweet しました");
      });
  }

  function showMe() {
      oauthSend(usersShowURL, 'get', model.oauth_token_secret, { "user_id": model.user_id }, {
          oauth_token: model.oauth_token,
          oauth_consumer_key: apiKey                    
      }, function (res) {
          const profile = JSON.parse(res.data);
          document.querySelector("#tw-profile").innerHTML = 
              "name: " + profile["name"] + "<br>" +
              "screen_name: " + profile["screen_name"] + "<br>" +
              "location: " + profile["location"] + "<br>" +
              "description: " + profile["description"] + "<br>";
          document.querySelector("#tw-profile-image").src = profile["profile_image_url_https"].replace("_normal", "");
      });
  }

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.

  const apiKey = '[Consumer Key(API key)]';
  const secretKey = '[Consumer Secret (API key secret)]'; 
  const callbackURL = '[Registered callback URL (scheme)]';

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.

To use the Twitter API, you need to register a developer account. Register a developer account from .

Go to .

We will use the plugin and a third-party plugin for single sign-on and use the library to create the signatures needed for OAuth.

Twitter Developer Account
the overview page of the Twitter Developer Portal
InAppBrowser
advanced-http
oauth-signature
InAppBrowser
advanced-http
oauth-signature
Import Twitter Single Sign-on App to your Monaca Account