Connect LinkedIn Account into your Rails Application with Authlogic and OAuth
取得連結
Facebook
X
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.
金面山 ,海拔667公尺,是 桃園小百岳之一 ,為大溪的最高峰,以其多變的地形和挑戰性的路線聞名。因山形尖銳酷似鳥嘴,又名「鳥嘴山」或「鳥嘴尖」,為大溪八景之一「 鳥嘴含煙 」。金面山步道亦是古道,又稱「金山面仔古道」,是昔日三峽五寮前往大溪買賣貨品的一條便道。 金面山步道是一條環狀路線,但也可以原路來回,步行時間約3小時,難度屬於低至中等。步道沿途林相自然,偶有大石、危崖、峭壁,需要拉繩攀爬,野趣十足。在稜線開闊處,視野還可以,可眺望大溪、龍潭、鴻禧山莊、武嶺橋等景觀。山頂路線四通八達,可規劃不同的行走方式,例如環形一圈順登十三分山。此外,每年桐花季時,步道遍地落花,景色相當浪漫。 金面山共有三個主要登山口,均位於通往阮家莊土雞城的道路上(建議從第三登山口進出,第一登山口路徑濕滑、且指示不明顯,下山容易滑倒。): 第一登山口 :位於土雞城前方約1公里處的路旁。 第二登山口 :位於土雞城停車場前。 第三登山口 :位於土雞城後方,經過三個兄弟的店面後即達。 登山口建議導航至「 阮家莊土雞城 」,該地有寬敞的停車場可供登山客使用。需注意,前往土雞城的道路較為狹窄蜿蜒,行車時請小心駕駛。此外,如果下山後想吃山產土雞,必須事先電話預約,當天現點一般來說不會有(今天本想外帶半隻,但三家店都說沒有)。 鄰近山頭: 金山面山: 又名金山,海拔363公尺,有三等三角點1122號,和樟林林界點石柱,由金面山走至金山面山,單程約一小時。 山麻坑山: 海拔339公尺,有磺補2176號基石,在金面山第一登山口附近,登頂只需10分鐘。 交通方式: 開車: 國道1號新竹系統出口下交流道,或國道3號大溪出口下交流道,接台3線往慈湖方向行駛,轉台7線約1.2K處,左轉桃58鄉道,依美華國小指標,再循阮家土雞城指標,續行4.7公里後,抵達金面山第一登山口、金面山第二登山口、金面山第三登山口。 大眾運輸: 桃園客運5097、5099、5107:自「大溪」站搭乘,於「美華國小」站牌下車。再循阮家土雞城指標步行4.7公里,或搭計程車前往登山口。 Jinmian Mountain, with an elevation of 667 meters, is one of Taiwan's Minor One Hundred Peaks and the highest peak in Daxi, ren...