Giter VIP home page Giter VIP logo

chrome-extension-twitter-oauth-example's Introduction

Chrome Extension Twitter Oauth Example

This is an example of a Chrome Extension that allows the user to perform a three-legged oauth into Twitter. It uses chrome.storage.local for storing the oauth tokens.

The chrome extension with twitter oauth flow

Setup

  1. Clone this repo.
  2. Point your Chrome browser to chrome://extensions/. Select developer mode.
  3. Click "Load unpacked extension..."
  4. Then select the directory where the manifest.json file lives

How it works

Because chrome extensions don't have a callback URL that you can provide Twitter to make the oauth process seamless, you have to use content scripts to inject a script onto the callback page that parses the tokens from the query string, and then sends it as a message to the background script.

Setting Up Your Twitter App

On your application management interface, be sure to add a callback URL in the field. It doesn't matter too much what URL you stick in here, so long as you update your manifest.json file to include that domain for when you inject your content script.

For this example, I will just use my personal domain, https://andyjiang.com/.

Adding my personal website as the callback URL

Note that if it's left blank, the oauth flow will default to a pin-based out-of-band oauth process.

Then, grab your consumer key and consumer secret from your application settings and paste them in js/lib/twitter.js:

  var consumer_key = 'XXXXXXXXXXXXXXXXX';
  var consumer_secret = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY';

This will let Twitter know which app is trying to authenticate when the user kicks off the oauth process.

Updating your manifest.json file

Your manifest.json file tells Chrome everything it needs to know about your extension, including permissions and which scripts to inject on which sites.

First, let's set the permissions. This is an array that tells Chrome what your extension can and cannot do:

  • tabs: for opening a new tab when we start the oauth process that directs users to their twitter login screen
  • storage: for saving tokens to chrome.storage.local
  • https://api.twitter.com/*: we need to make requests to this endpoint for the oauth process
    "permissions": [
        "tabs",
        "storage",
        "https://api.twitter.com/*"
    ],

Next, let's define how our content scripts are used. You need to have js/session.js load on https://andyjiang.com/*, because when Twitter redirects the user to that domain provided in the callback URL, js/session.js will parse the tokens from the query string and send it to your js/background.js file.

    "content_scripts": [{
      "matches": ["https://andyjiang.com/*"],
      "js": ["js/session.js"]
    }]

If you take a look at js/session.js, you can see all it does is parse the querystring from the URL and send it to the background script as a message. Afterwards, it closes the tab automatically.

chrome.runtime.sendMessage({type: 'auth', session: window.location.search.substr(1)}, function(response) {
    window.open('', '_self', '');
    window.close();
});

Finally, we have to make sure chrome is made aware of all of the background scripts. If we omit one, then it won't be included, even if it's referenced in background.html.

  "background": {
    "scripts": [
      "js/lib/jquery.min.js",
      "js/lib/oAuth.js",
      "js/lib/sha1.js",
      "js/lib/twitter.js",
      "js/background.js"
    ],
    "persistent": false
  },

There you have it.

Find me on twitter if you have any questions or comments!

chrome-extension-twitter-oauth-example's People

Contributors

lambtron avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chrome-extension-twitter-oauth-example's Issues

Chrome Manifest v3?

Would you consider updating this extension to conform to the new v3 manifest for Chrome extensions? Chrome already won't load this extension as-is right now, and manifest v2 will be completely deprecated in 2023.

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.