• This repository has been archived on 23/Jan/2018
  • Stars
    star
    379
  • Rank 108,792 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 15 years ago
  • Updated almost 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿšซ DEPRECATED - Use https://github.com/sferik/twitter

Twitter OAuth REST API client library for Ruby

Current Status

Twitter turned off access to the v1 API so this project is currently migrating to v1.1. Unfortunately everything is not backward-compatible.

The v.0.4.9x series of this gem is work in progress getting the library compatible with Twitter v1.1 โ€“ when complete we will push v0.5.0 of this library.

Install the gem

gem โ€˜twitter_oauthโ€™

Using the gem

To make authorized requests with the client library youโ€™ll need to create a Twitter OAuth Application.

See sinitter for a full website integration example.

Unauthorized request example

Since Twitter API v1.1 all requests are required to be authorized.

Authorized request example

To use the full power of the Twitter API you need to authorize your application and a valid Twitter user via OAuth.
An example showing how to update the status of an authorized user is below.

Firstly we need to create an instance of the client with your application client credentials you have been given by Twitter
when you set up your application.

client = TwitterOAuth::Client.new(
    :consumer_key => 'YOUR_APP_CONSUMER_KEY',
    :consumer_secret => 'YOUR_APP_CONSUMER_SECRET'
)
request_token = client.request_token(:oauth_callback => oauth_confirm_url)
#:oauth_callback required for web apps, since oauth gem by default force PIN-based flow
#( see http://groups.google.com/group/twitter-development-talk/browse_thread/thread/472500cfe9e7cdb9/848f834227d3e64d )


request_token.authorize_url
=> http://twitter.com/oauth/authorize?oauth_token=TOKEN

In your application your user would be redirected to Twitter to authorize the application at this point. Youโ€™ll need to store
the request token (usually in the session) for later. The code continues below assuming the user has authorized your application.

NOTE: Above we called the client.request_token(...) method, this authorizes the application on every call. You can also use the client.authentication_request_token(...) method which automatically redirects back to your application if the user has previously authorized the app.

access_token = client.authorize(
  request_token.token,
  request_token.secret,
  :oauth_verifier => params[:oauth_verifier]
)

client.authorized?
=> true

client.update('checking out the twitter_oauth library') # sends a twitter status update

Now if you keep hold of the access_token (usually in the database) for this user you wonโ€™t need to re-authorize them next time. When you create an instance of the client you can just pass in the access token and secret that you have stored.

access_token = @user.access_token # assuming @user
client = TwitterOAuth::Client.new(
    :consumer_key => 'YOUR_CONSUMER_KEY',
    :consumer_secret => 'YOUR_APP_CONSUMER_SECRET',
    :token => access_token.token,
    :secret => access_token.secret
)

client.authorized?
=> true

PIN-based flow

If youโ€™re writing a command line application or desktop app, you will probably want to use the PIN-based authorization method rather than the website redirect method.

client = TwitterOAuth::Client.new(
  :consumer_key => 'YOUR_CONSUMER_KEY',
  :consumer_secret => 'YOUR_APP_CONSUMER_SECRET'
)

request_token = client.authentication_request_token(
  :oauth_callback => 'oob'
)

puts request_token.authorize_url

print 'Please visit the URL and enter the code: '
code = gets.strip

access_token = client.authorize(
  request_token.token,
  request_token.secret,
  :oauth_verifier => code
)

client.authorized?
=> true

The special oauth callback value of oob tells Twitter you want to do the PIN-based authentication. The user goes to the authorization URL to get their unique code and they paste that into your application. Finally we authorize the user with this code.

Working with a Proxy

Services such as Apigee Analytics and API Management require you to proxy your API requests through their servers. The workflow is as follows.

First you need to authorize the Twitter user via OAuth directly via the Twitter API (this part cannot be proxied)

client = TwitterOAuth::Client.new(
    :consumer_key => 'YOUR_APP_CONSUMER_KEY',
    :consumer_secret => 'YOUR_APP_CONSUMER_SECRET'
)
request_token = client.request_token(:oauth_callback => 'YOUR_CALLBACK_URL')

request_token.authorize_url
=> http://twitter.com/oauth/authorize?oauth_token=TOKEN

The user is sent to Twitter to allow the application to access their account and then you need to obtain the access token for the user

access_token = client.authorize(
  request_token.token,
  request_token.secret,
  :oauth_verifier => params[:oauth_verifier]
)

client.authorized?
=> true

Now you can make all further API calls through the proxy of your choice:

access_token = @user.access_token # assuming @user
client = TwitterOAuth::Client.new(
    :proxy => 'http://XXX.YYY.apigee.com',
    :consumer_key => 'YOUR_CONSUMER_KEY',
    :consumer_secret => 'YOUR-CONSUMER-SECRET',
    :token => access_token.token,
    :secret => access_token.secret
)

client.authorized?
=> true

client.update('Proxy via Apigee is working')

More Repositories

1

facebook_oauth

๐Ÿšซ DEPRECATED - Use https://github.com/arsduo/koala
Ruby
90
star
2

flappy-phoenix

๐Ÿฆ Flappy Bird clone written in Elixir using Phoenix LiveView to render the game UI from the server.
JavaScript
74
star
3

libcluster_tailscale

A libcluster strategy for discovering and connecting Elixir nodes over Tailscale
Elixir
73
star
4

gstore

๐Ÿšซ DEPRECATED - Use https://github.com/GoogleCloudPlatform/google-cloud-ruby
Ruby
59
star
5

two_factor_auth_rails

Demo of a two-factor authentication implementation for Rails
Ruby
25
star
6

Sounds

๐Ÿ”Š BBC Sounds status bar app for macOS
Swift
14
star
7

newrelic-in-motion

Sample project showing working NewRelic iOS App Monitoring Integration
Ruby
10
star
8

zap

โšก Zap - A development web/proxy server that super-charges your development experience
Go
9
star
9

snowball

Sample Elixir/Phoenix application replacing Webpack with Snowpack
Elixir
8
star
10

google_oauth

๐Ÿšซ DEPRECATED - Use https://github.com/google/google-api-ruby-client
Ruby
8
star
11

aws

Amazon Web Services client library for Ruby implementing the EC2, Cloud Watch, Elastic Load Balancing and Auto Scaling APIs
Ruby
7
star
12

atproto

Implementation of the ATProtocol client spec in Elixir.
Elixir
5
star
13

phoenix-gitpod

Example Gitpod configuration for Phoenix development
Elixir
4
star
14

stytch-elixir

An Elixir API client for Stytch
Elixir
3
star
15

rambo

An alternative web framework for Ruby
Ruby
3
star
16

opi

The very opinionated API service library
Ruby
3
star
17

actions

Github Actions
Dockerfile
3
star
18

go-lib

๐Ÿ›  A collection of useful reusable go packages
Go
2
star
19

moomerman.github.com

2
star
20

motion-radio

๐Ÿ”Š macOS streaming radio system tray application
Ruby
2
star
21

xrpc

Implementation of the XRPC client spec in Elixir.
Elixir
1
star
22

songdrop_ruby

Ruby & RubyMotion API client for songdrop.com
Ruby
1
star
23

go-images

Fast, reliable file upload to S3 and dynamic image manipulation service written in go
Go
1
star
24

aoc2023

Advent of Code 2023
Elixir
1
star