Twitter 3-Legged Authorization in Ruby

Posting to another user's twitter account from my app

Since you are instantiating the Twitter::REST::Client in an initializer using a fixed set of credentials, I'd assume that the client is always using the credentials with which it was instantiated.

In order to post a tweet on behalf of an Account, you'll want to instantiate a client that utilizes the credentials you are receiving from authorization, something like this:

class Account < ActiveRecord::Base
def twitter_client
Twitter::REST::Client.new do |config|
config.consumer_key = "2BN4jYtsf453374VM4m7HG"
config.consumer_secret = "LOE545ksfjkae3r8FOzj3NTHXVBfx8njPX0JHMNqCJs3mOnhQleH"
config.access_token = self.token
config.access_token_secret = self.secret
end
end
end

Then given an account, you could post like so:

account.twitter_client.update("Just setting up my twttr")

Twitter - 3-legged OAuth

Use the Sign-in With Twitter flow, and store the access token that the user receives when they grant access to your app. Then there's no need to go down the oauth/authorize route every time.

use twitter iOS client authorization on own server

To answer my own question:
The process described in the question works. The only thing that I corrected is the consumer_key and consumer_secret.
Since the access_token/secret of the user were generated by the iOS SSO, the consumer_key and consumer_secret needed were not the ones of your rails app, but the one of your iOS app (that you can find in Fabric admin).

Then, the twitter client in your rails app can send back all relevant information about the user in your database.

Twitter Bot: Get access token for another account without 3-legged OAuth flow?

There are some alternative ways to generate access token and access token secret for OAuth 1.0A via command line tools which allow you to use the “PIN-based” OAuth flow.

One example would be Twitter’s own twurl tool for API testing, which requires you to also have Ruby installed. This will let you authenticate a user account (it still pops open a window onto twitter.com to have you do the authentication) and stores them into the ~/.twurlrc file in your home directory. There is also tw-oob-oauth-cli which is a standalone app for doing the same thing.

3-legged OAuth and one-time-code flow using google-auth-library-ruby with google-api-ruby-client

After a good amount of digging through code samples and source code, I have a clean working solution. Once I found the page in my "update" it led me to finding out that ClientSecrets way I was doing things had been deprecated in favor of the google-auth-library-ruby project. I'm glad I found it because it seems to be a more complete solution as it handles all of the token management for you. Here is code to setup everything:

def authorizer
client_secrets_path = File.join(Rails.root,'config','client_secrets.json')
client_id = Google::Auth::ClientId.from_file(client_secrets_path)
scope = [Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY]
redis = Redis.new(url: Rails.application.secrets.redis_url)
token_store = Google::Auth::Stores::RedisTokenStore.new(redis: redis)
Google::Auth::WebUserAuthorizer.new(client_id, scope, token_store, "postmessage")
end

and then this is how I use the authorization code:

def exchange_for_token(user_id,auth_code) 
credentials_opts = {user_id:user_id,code:auth_code}
credentials = authorizer.get_and_store_credentials_from_code(credentials_opts)
end

after calling that method the library will store the exchanged tokens in Redis (you can configure where to store) for later use like this:

def run_job(user_id)
credentials = authorizer.get_credentials(user_id)
service = Google::Apis::CalendarV3::CalendarService.new
service.authorization = credentials
calendar_list = service.list_calendar_lists.items
# ... do more things ...
end

There is so much info out there that it is difficult to isolate what applies to each condition. Hopefully this helps anyone else that gets stuck with the "one-time-code flow" so they don't spend days banging their head on their desk.

Twitter ruby gem .search request multiple responses

As docs state,

:count (Integer) — The number of tweets to return per page, up to a
maximum of 100.

So, that is kinda reverse for what you're trying to achieve: the more count is, the less http requests you gonna make (as each of them would contain more tweets). Note that you can not set count to an amount more than 100, so the only way to restrict amount of requests in case there is a huge amount of tweets would be

search_results.take(requests_amount)
# => made requests_amount requests, returns requests_amount pages
# each of page should contain up to count tweets, with max of 100


Related Topics



Leave a reply



Submit