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: 😎
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:
compile ":oauth:2.6.1"
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:
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" } } }
Step 4: Add Yahoo link in your page
Add oauth:connect tag of OAuth plugin tag library to create a link to Yahoo.
<oauth:connect provider="yahoo" title="Yahoo">Yahoo</oauth:connect>
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.
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}" ... } }
and you are done, enjoy..,. 🙂
I have deployed a demo project in Heroku.
See this code in action (DEMO).
Hope this helps. 🙂
Recent Comments