Connect LinkedIn Account into your Rails Application with Authlogic and OAuth
取得連結
Facebook
Twitter
Pinterest
以電子郵件傳送
其他應用程式
LinkedIn launched its new API in November 2009, allowing any developer brave enough to implement OAuth to integrate with one of the largest social networks around. Seeking an opportunity to really understand OAuth more fully and play around with the LinkedIn API at the same time, I created the LinkedIn connect sample code.
This is a sample Ruby on Rails tutorial to demonstrate how you can use the LinkedIn API to authenticate users and pull their profile data into your own website.
GETTING STARTED
Before you can use the LinkedIn API, you'll need to set up your API keys.
1. Where do I begin? Start by navigating to http://developer.linkedin.com. This is Linkedin developer portal where you can read API documentation, collaborate with other developers, and keep abreast of changes to the LinkedIn API. From here, click on “LinkedIn APIs.”
2. Now click on “Request an API Key”
3. After logging in, you’re now presented with an opportunity. You can create as many API keys as you like, one for each application that you create. Let’s get started.
4. This isn’t as complicated as it looks. Let’s look closely.
Application Developers: This is a list of your LinkedIn Connections that will receive network updates published by your application and is used only while it is in development mode. You need to be connected to any LinkedIn member that you want to put on this whitelist. Interface Language: Your application is provided in one or more languages to end users. Let us know what those languages are so we can better support you in the future. Programming Tools: Knowing what kind of programming languages and tools you use to develop your application lets us know what languages and environments are most important for us to support. Just a few more fields to go over!
5. Ready to get your API keys? When you’re done, click “Add Application”
Contact Info: Tell us how to get in contact with you should we have a question about or there is a problem with your application.
OAuth Redirect URL: If you have a static location that should always be used for your OAuth callbacks, you can specify it here. It is recommended that you use the oauth_callback parameter in the appropriate requests instead.
Agreement Language: If you’d like to force the login screen to appear in one of the languages LinkedIn supports, you can set that here. It is recommended to leave the setting at “Browser Locale Setting.”
Terms of Service/CAPTCHA: Please do read our API Terms of Use. You cannot create an API key without agreeing to our terms. You’ve likely seen a CAPTCHA before, and it needs little explanation.
6. You’ve got your API keys. What’s next?
The LinkedIn Developer Network is where you’ll find guidelines, documentation, and growing community of developers.
RUBY GEM INSTALLATION
First you need to upgrade your Rails VERSION to 2.3.x, and install the required Ruby Gems below:
1. Setup and use Authlogic
$ sudo gem install authlogic
2. Install OAuth and Authlogic_Oauth
$ sudo gem install oauth
$ sudo gem install authlogic-oauth
3. Install other required Ruby gems
$ sudo gem install json
$ sudo gem install crack
Or for older version of Rails, you can install it as a plugin:
2. Create Position,Education and Connection model:
$ script/generate model position
$ script/generate model education
$ script/generate model connection
3. Modify the User model, add the content below to the #{RAILS_ROOT}/app/models/user.rb:
The private method "populate_oauth_user", which is called before user record created, and is used to fetch profile info (name, headline, industry, profile pic, public url, summary, specialties, web urls) in LinkedIn.com, and save these info to your user record. The other private method "populate_child_models", which is called after user record created, and which will save past position info (companies, job titles, durations, descriptions), education info (schools, degrees, field of study, dates, etc) and connections info (names, industries, headlines, profile pics) for user.
4. Modify the Connection model, add the contents below to the #{RAILS_ROOT}/app/models/connection.rb:
5. Modify the Education model, add the content below to the #{RAILS_ROOT}/app/models/education.rb:
6. Modify the Position model, add the content below to the #{RAILS_ROOT}/app/models/position.rb:
7. Create UserSession model, and define the oauth_consumer class method on your UserSession model:
CONTROLLER METHOD
You only need to save your objects this way if you want the user to authenticate with their OAuth provider.
That being said, you probably want to do this in your controllers. You should do this for BOTH your User objects and UserSession objects (assuming you are authenticating users). It should look something like this:
You should save your @user objects this way as well, because you also want the user to authenticate with OAuth.
In my case, I have to integrating the LinkedIn OAuth Sign in feature to my current Rails project, so I changed the "Create" method in my UsersController like this way:
When the user click on the LinkedIn Login button and we can't find the oauth_token and oauth_secret in the database, the user will be redirect to signup page with "linkedin_user" parameter. After fill in the fields, they will be redirect to LinkedIn authentication page, then user record will be created. If we can find the oauth_token and oauth_secret in the database, the user will be login to the site.
ADD REGISTER BUTTON
You can simply use the "oauth_register_button" helper method to add LinkedIn register buttons to your views, like this:
Now add some style in your CSS file for LinkedIn button:
NEXT STEPS
Now we have user's LinkedIn data saved in the database, so you can write some codes to show the user's LinkedIn profile data (such as Username, Headline, Location, Education, Position, Websites, Skills, Experience, Connections) in the website.
You may want to add methods to update profile info on each login or update request. Or pull status updates, specialties, and current position for connection.
作者約翰·D·洛克菲勒 John D. Rockefeller(1839—1937年) 美國實業家、超級資本家,美孚石油公司(標準石油)創辦人。出生於紐約州裏奇福德鎮,父親威廉·埃弗裏·洛克菲勒是一個無牌遊醫,母親伊萊紮·戴維森是一個虔誠的浸理會教徒。由於其父缺乏責任心,長年在外以藥販身份流浪,導致家庭生活艱難。母親伊萊紮肩負起養家糊口的繁重任務,獨自撫養五個子女。幼年時,曾將自己捉到的小火雞精心餵養,挑好的在集市上出售。12歲時積蓄了50美元,他把錢借給鄰居,收取本息。 1855年,由於其父的原因不能繼續上學,離7月16日高中畢業典禮只差兩個月。約翰聽從父親建議,花了40塊錢在福爾索姆商業學院克裏夫蘭分校讀了一個為期三個月的課程。16歲的洛克菲勒在美國俄亥俄州的一家幹貨店當職員,每星期賺5美元。19歲,他下海經商,倒賣谷物和肉類。從這時起,洛克菲勒將每一筆收支記錄在冊,甚至不漏掉一個便士的慈善捐款。經過三年積累,22歲的洛克菲勒進入石油業,並於1870年創建標準石油公司。洛克菲勒成了蜚聲海內外的“石油大王”。 1897年,從標準石油公司退休後,洛克菲勒專註於慈善事業。中國在洛克菲勒基金會的其海外投資中獨占鰲頭。眾所周知的是,北京協和醫院及醫學院是洛克菲勒基金會在中國最大、最著名的一項事業;鮮為人知的是,周口店“北京人”的挖掘和考古工作,洛克菲勒基金會從一開始就參與其中。 對於中國人來說,“富不過3代”似乎是鐵一樣的定律,然而洛克菲勒家族從發跡至今已經綿延6代,仍未現頹廢和沒落的跡象。這與他們的財富觀念和從小對子女的教育息息相關。他們的家族崇尚節儉並熱衷創造財富。這兩點從洛克菲勒家族的中興之主勞倫斯·洛克菲勒的一生中體現得尤其充分。 2004年7月11日,坐擁億萬家財、在美國叱咤風雲的勞倫斯·洛克菲勒在睡夢中與世長辭,享年94歲。這位洛克菲勒家族的第3代傳人,盡管含著金鑰匙出生,卻絕不是一位紈絝子弟,他在有生之年不僅開了風險投資的先河,還為美國的環保及慈善事業作出了不朽的貢獻。 老約翰·D·洛克菲勒惟一的兒子和繼承人是小約翰·D·洛克菲勒。小約翰·D·洛克菲勒共有6個子女,姐姐芭布斯最大,其他都是男孩,從大到小分別是約翰、納爾遜、勞倫斯、溫斯羅普和大衛。勞倫斯·洛克菲勒1910年5月26日生於紐約。童年時期,勞倫斯與年長他兩歲的納爾遜關系最親密,他們曾一