Skip to content

Instantly share code, notes, and snippets.

@tylerhunt
Created March 30, 2021 18:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerhunt/4b4fa198f4d42b9f7b81c84cf7df6ab6 to your computer and use it in GitHub Desktop.
Save tylerhunt/4b4fa198f4d42b9f7b81c84cf7df6ab6 to your computer and use it in GitHub Desktop.
Generates a 2FA QR code for Instagram.

Instagram 2FA QR Code Generator

Instagram doesn’t make it easy to enable 2FA unless you’re using Google Authenticator or Duo Security. If you want to use it with another authenticator app—like 1Password—this script will generate the QR code you need to easily set that up.

Usage

The Ruby script makes use of Bundler’s inline functionality and will automatically install the depenedncies as system gems.

ruby qr.rb

You’ll be prompted for your Instagram username and the secret which can be generated through the Instagram app. A QR code will be generated using SVG and opened for scanning by your authenticator app.

require 'bundler/inline'
require 'tempfile'
gemfile do
source 'https://rubygems.org'
gem 'addressable', '~> 2.7', require: 'addressable/template'
gem 'highline', '~> 2.0'
gem 'launchy', '~> 2.5'
gem 'rqrcode', '~> 1.2'
end
template = Addressable::Template.new(
'otpauth://totp/Instagram%3A%40{username}?secret={secret}&issuer=Instagram'
)
cli = HighLine.new
username = cli.ask('Username: ')
secret = cli.ask('Secret: ') { |q| q.echo = false }
uri = template.expand(username: username, secret: secret)
qr_code = RQRCode::QRCode.new(uri.to_s)
svg = Tempfile.create(['instagram_', '.svg'])
svg << qr_code.as_svg
Launchy.open svg.path
@mir-masroor
Copy link

hi, im new to ruby and have tried using your script but it automatically closes the terminal and i cant find the svg file, where is it stored?

@tylerhunt
Copy link
Author

@Sammerimer Sounds like the Launchy.open call might be failing. You could try changing the last line to puts svg.path to have it output the SVG path instead of trying to open it.

@ronilaukkarinen
Copy link

It works, thanks!

@kirkegaard
Copy link

What exactly is the secret in this case?

@tylerhunt
Copy link
Author

tylerhunt commented Dec 24, 2022

@kirkegaard The secret needs to be generated with the Instagram mobile app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment