I am back with a new OAuth blog, topic: Integration with Yahoo using Grails OAuth Plugin.
So far, we have successfully integrated various OAuth providers in our application: 😎
- Google (OAuth 1.0)
- Google (OAuth 2.0)
Let’s try one more OAuth provider, Yahoo.
Goal: Integrate with Yahoo using Grails OAuth plugin and get the user details.
Step 1: Install the OAuth plugin
Open BuildConfig.groovy and add the following dependency inside plugins section:
1 2 3 |
[code] compile ":oauth:2.6.1" [/code] |
Step 2: Sign up as Yahoo developer and create a new application
Go to the link and complete the application form. After successfully creating the app you got the Consumer Key and Consumer Secret.
Step 3: Add Config values in Config.groovy
Add following code in you Config file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[code] oauth { providers { yahoo { api = YahooApi key = 'YOUR_CONSUMER_KEY' secret = 'YOUR_CONSUMER_SECRET' callback = "${grails.serverURL}/oauth/yahoo/callback" successUri = "${grails.serverURL}/oauthCallBack/yahoo" failureUri = "${grails.serverURL}/oauthCallBack/failure" } } } [/code] |
Step 4: Add Yahoo link in your page
Add oauth:connect tag of OAuth plugin tag library to create a link to Yahoo.
1 2 3 |
[code] <oauth:connect provider="yahoo" title="Yahoo">Yahoo</oauth:connect> [/code] |
Step 5: Add handler for Yahoo response
Final step is to create action to handle the response returned by the Yahoo after successful authentication then make request to get the user details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[code] import org.scribe.model.Token class OauthCallBackController { def yahoo() { Token token = (Token) session[oauthService.findSessionKeyForAccessToken('yahoo')] String guidUrl = "https://social.yahooapis.com/v1/me/guid?format=json" def yahooResource = oauthService.getYahooResource(yahooAccessToken, guidUrl) def yahooResponse = JSON.parse(yahooResource?.getBody()) String userInfoUrl = "https://social.yahooapis.com/v1/user/${yahooResponse?.guid?.value}/profile/usercard?format=json" yahooResource = oauthService.getYahooResource(yahooAccessToken, userInfoUrl) yahooResponse = JSON.parse(yahooResource?.getBody()) def yahooProfile = yahooResponse.profile log.info "token = ${token}" log.info "Guid = ${yahooProfile.guid}" log.info "Nickname = ${yahooProfile.nickname}" ... } } [/code] |
and you are done, enjoy..,. 🙂
I have deployed a demo project in Heroku.
See this code in action (DEMO).
Hope this helps. 🙂
Recent Comments