Coder Social home page Coder Social logo

guylando / twitterkit3-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chroa/twitterkit3-plugin

5.0 1.0 5.0 28.54 MB

A WORKING version of a Cordova plugin allowing Twitter Single Sign On Login (using Latest Twitter Kit versions)

License: Apache License 2.0

Java 5.41% Objective-C 93.96% C 0.31% JavaScript 0.32%

twitterkit3-plugin's Introduction

twitter-plugin

Using Twitter Kit 3

Cordova/PhoneGap plugin to use Twitter Single Sign On

Available on npm at: https://www.npmjs.com/package/cordova-twitter3-connect-plugin

Install

Create a Twitter app

Create a Twitter application in https://apps.twitter.com and get the consumer key and secret under the "Keys and Access Tokens" tab.

IMPORTANT OR OTHERWISE LOGIN WILL FAIL: Make sure that the Callback URL contains:

twittersdk://
twitterkit-CONSUMERKEY://

See: https://developer.twitter.com/en/docs/basics/apps/guides/callback-urls.html

(replace CONSUMERKEY by your twitter app consumer key)

If desired to see the user's email (for example, by using the verify credentials endpoint), the "Additional Permissions" (in the "Permissions" tab) needs to be checked. Consequently, fill in the respective Privacy Policy URL and Terms of Service URL fields.

Add plugin to your Cordova app

Make sure you put in your valid API keys in their respective place.

cordova plugin add https://github.com/guylando/twitterkit3-plugin --variable TWITTER_KEY=<Twitter Consumer Key> --variable TWITTER_SECRET=<Twitter Consumer Secret>

IMPORTANT: This plugin has all necessary url scheme handling built-in and does not require you to add anything to your ios AppDelegate. However some plugins such as https://github.com/jeduan/cordova-plugin-facebook4 have openUrl code which prevents this plugin openUrl to run resulting in the login not finalizing after coming back to the app from the twitter webview. So if such problems are encountered then check which plugins are you using and fix the bug there. See: jeduan/cordova-plugin-facebook4#166

Usage

This plugin adds an object to the window named TwitterConnect. The following methods are provided by the plugin.

Login

Login using the .login method:

TwitterConnect.login(
  function(result) {
    console.log('[Login] - Successful login!');
    console.log(result);
  },
  function(error) {
    console.log('[Login] - Error logging in: ' + error);
  }
);

The login reponse object is defined as follows.

{
  userName: '<Twitter User Name>',
  userId: '<Twitter User Id>',
  secret: '<Twitter Oauth Secret>',
  token: '<Twitter Oauth Token>'
}

NOTE: This plugin has a fix to a bug appearing in the other twitter cordova plugins which returned userId as an integer which is fine in java but gets truncated in javascript because its too big for a javascript integer and thus this plugin fixed it by converting the user id to string before passing it to javascript which insures that correct userId is retrieved.

See:

https://twittercommunity.com/t/twitter-id-and-id-str-are-different/79942

https://developer.twitter.com/en/docs/basics/twitter-ids

https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object

NOTE: TwitterKit saves user id as a long and doesnt provide it as a string so need to convert to string before sending it to javascript

https://github.com/twitter/twitter-kit-android/blob/master/twitter-core/src/main/java/com/twitter/sdk/android/core/TwitterSession.java#L48

https://github.com/twitter/twitter-kit-android/blob/master/twitter-core/src/main/java/com/twitter/sdk/android/core/Session.java#L45

https://github.com/twitter/twitter-kit-android/blob/master/twitter-core/src/main/java/com/twitter/sdk/android/core/identity/AuthHandler.java#L91

https://github.com/twitter/twitter-kit-android/blob/master/twitter-core/src/main/java/com/twitter/sdk/android/core/models/User.java#L128

https://github.com/twitter/twitter-kit-android/blob/master/twitter-core/src/main/java/com/twitter/sdk/android/core/internal/oauth/OAuthResponse.java#L52

Logout

Logout using the .logout method:

TwitterConnect.logout(
  function() {
    console.log('[Logout] - Successful logout!');
  },
  function() {
    console.log('[Logout] - Error logging out');
  }
);

ShowUser

Show a user's profile information. Returns a JSON object as specified in the API: https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-show

TwitterConnect.showUser(

  /*Endpoint arguments*/
  {"include_entities" : false},

  /*Callback functions*/
  function(result) {
    console.log('[ShowUser] - Success!');
    console.log(result);
    console.log('[ShowUser] - Twitter handle: ' + result.screen_name);
  },
  function(error) {
    console.log('[ShowUser] - Error: ' + error);
  }
);

VerifyCredentials

Show's a user's profile information with added details as specified. Returns a JSON object as specified in the API: https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/get-account-verify_credentials

TwitterConnect.verifyCredentials(

  /*Endpoint arguments*/
  {"include_entities": false,
  "skip_status" : true,
  "include_email": true},

  /*Callback functions*/
  function(result) {
    console.log('[VerifyCredentials] - Success!');
    console.log(result);
  },
  function(error) {
    console.log('[VerifyCredentials] - Error: ' +  error);
  }
);

twitterkit3-plugin's People

Contributors

guylando avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.