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.
大年初二我們來到了位在苗栗三義老街附近,一間既是美術館也是餐廳的預約制餐廳【富貴牡丹人文藝術餐廳】用餐。富貴牡丹有兩個館區,分別是 鶯歌富貴館 ,以及 富貴三義館 , 三義館聽聞已開幕十年以上,是一間環境和用餐氛圍都相當不錯的餐廳。 隱匿在三義翠綠山林中的「 富貴三義美術館 」是一個可以同時享受藝術、自然和建築的秘境!美術館位於苗栗,擁有壯麗的山巒美景,除了作為展示藝術品的場所,整體建築也與周圍的自然環境和諧共處。館內的鏡面水池倒映四季變幻,呈現出不同的風采。 富貴三義美術館成立於 2011 年,在提供藝文體驗的同時,也讓訪客在森林中享用美食。美術館依地形高低差建造,營造出深度的藝術觀賞體驗。這座山中美術館的設計籌備歷時三年、修築構建四年,共計七年才建造完成。 美術館以「 富於心,貴於情 」為核心理念,鼓勵每位訪客用心感受藝術、體會生活。在這裡,藝術不再遙不可及,而是觸手可及的日常。 室內室外寬敞的環境中,有擺放許多精緻的藝術家雕塑作品,結合藝術、自然、美食,就像在美術館裡頭用餐一樣,是一場令人難忘的美食感官饗宴。 富貴牡丹餐廳只有45個座位,提供歐亞料理,搭配藝術家創作的器皿,在安靜舒適的環境中,享受美食饗宴,還能欣賞窗外的自然景色,以最貼近生活的方式,將藝術、美食,呈現給大家。 On the second day of the Lunar New Year, we dined at FUGUEI Peony Art Restaurant, a reservation-only restaurant that also serves as an art museum, located near the old street of Sanyi, Miaoli. Fu Gui Mu Dan has two branches: Yingge Fu Gui Museum and FUGUEI Sanyi Museum. It is said that the Sanyi branch has been open for over ten years and offers both a great dining atmosphere and a beautiful environment. Hidden within the lush green mountains...
週末林口在下毛毛雨,查了天氣預報新竹天氣還不錯,下午決定跑一趟尖石鄉,去走一趟 煤源山、比麟山、尖石山 連走,這個健行路線,有人稱之為 煤比尖連走 (也被稱為 凌尖縱走 ),個人走完全程來回大約三個半小時(建議抓五個小時)。 從凌空廊道起登,由越嶺稜線路,經煤源山、比麟山到尖石山原路往返。這條路線一路都走在森林中,因為有登山隊整理過山徑,路跡還算是清晰。全程海拔高度的起伏不大,陡上下的路段大概就幾十公尺的高度落差,大部分路段都是平緩的原始山徑,是相對輕鬆,且林相優美的中級山路線。 今天下午才從登山口起登,全程途中都沒碰到半個人,只有在比麟山往尖石山中間地點,碰見一群團體正要下山,後來我登頂下山後,又在煤源山附近再次碰到他們。 下山後,我在路旁看見一家由紅磚矮房建造的原住民料理餐廳,決定先吃個晚餐,因為已經肚子餓了,進門前也忘了查詢評價如何,不過出乎預料之外的美味,下次若有機會來這附近爬山,會考慮再來這裡用餐。 It was drizzling lightly in Linkou over the weekend. I checked the weather forecast, and it looked good in Hsinchu, so in the afternoon, I decided to take a trip to Jianshi Township to hike the route connecting Meiyuan Mountain, Biling Mountain, and Jianshi Mountain. Some call this the Meibi-Jian traverse, while others refer to it as the Lingjian traverse. I personally completed the whole route in about three and a half hours (but it's recommended to allow five hours). The hike starts from the Lingkong Corridor, following the ridgeline trail, passing Meiyuan Mountain, Biling Mou...
今天是個晴朗的午後,家人從台中北上,下午我們前 往大棟山 附近,一家隱藏在山林間的寵物友善咖啡廳「 樹頂上 TREE TOP KAFFA 」用餐。 這家需要預約的咖啡店,位於 新北市樹林區德和街17巷15號 ,距離大棟山405高地不遠,從桃園市區開車或騎機車上山約20幾分鐘就能抵達。TREE TOP KAFFA 的營業時間平日從上午11點到下午5點,週末則延長到晚上7點,剛好適合午後小憩或欣賞夕陽轉夜景的轉變。 走進樹頂上,第一眼就被那片郁郁蔥蔥的森林包圍,彷彿進入了個私人秘境。主建築是兩層小平房,一樓室內空間溫馨舒適,二樓有戶外高腳椅座位區,可以遠眺層層山巒、三峽、鶯歌地區以及遠方的白雞山系。白天是綠意盎然的森林秘境,入夜後則化身療癒的星空座席,滿山燈火如繁星般閃爍,讓人心曠神怡(但蚊子非常多,所以今天我們選擇一樓的室內區用餐)。 TREE TOP KAFFA 不只賣咖啡和飲料,還有蛋糕、輕食披薩、炸物等簡餐,低消只需一杯飲料,用餐時間若客滿則限兩小時。他們的披薩和台式炸物,以及甜點都頗有水準,是可以用好吃形容的那種,而咖啡、水果茶等飲品也都相當不錯。週末如果不想跑太遠,這裡很適合上山來放鬆(這裡是寵物友善餐廳,但是用餐需事先電話預約)。 整體來說, 樹頂上 TREE TOP KAFFA 像是一篇山林日記的註腳,讓人在忙碌中找到片喘息空間。這裡不僅適合登山客短暫停留喝杯咖啡,也適合三五好友在此度過悠閒的下午茶時光。有機會不妨上山走走,親自體驗這份樹頂上的愜意。 TREE TOP KAFFA 樹頂上 營業電話:0918-701-883 營業地址: 新北市樹林區德和街17巷15號 營業時間:週一~五 11:00–17:00(週六、日營業到19:00) 跟家人用完餐後,我獨自前往 大棟山405高地 ,趁著還沒天黑,來一趟午後的散步。本來是計畫走到青龍嶺往返,但是走到青龍亭時,已經接近下午5點,於是決定趁早返回。從405高地到青龍亭,來回大約1小時20分鐘,回到405高地正好看夕陽。 大棟山 位在新北樹林與桃園龜山的交界處,是「 微笑山線 」登山步道系統中一顆璀璨的節點,串連起大同山與青龍嶺,成為健行者、自行車騎士、攝影愛好者乃至尋求心靈平靜的訪客們共同的目的地 。 大棟山擁有一等三角點及一圖根點,為林口台地稜脈的第一峰,名列台灣小百岳。此處坐擁極佳視野,除了可盡覽大...