Question

Its grails project,
Twitter Authentication is successful,
How to get twitter userID from this oauth_token received?
Thanks

Was it helpful?

Solution

Try this..,.

Config:

import org.scribe.builder.api.TwitterApi
...
oauth {
  providers {
    twitter {
        api = TwitterApi
        key = 'YOUR_KRY'
        secret = 'YOUR_SECRET'

        callback = "http://localhost:8080/appName/oauth/twitter/callback"

        successUri = "http://localhost:8080/appName/myController/twitterSuccess"
        failureUri = "http://localhost:8080/appName/myController/twitterFailure"
    }
  }
}

MyController:

def twitterSuccess() {
    Token twitterAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('twitter')]
    def twitterResource = oauthService.getTwitterResource(twitterAccessToken, "https://api.twitter.com/1.1/account/settings.json")
    def twitterResponse = JSON.parse(twitterResource?.getBody())

    def twitterResourceDetailed = oauthService.getTwitterResource(twitterAccessToken, "https://api.twitter.com/1.1/users/show.json?screen_name=${twitterResponse['screen_name']}")
    def twitterResponseDetailed = JSON.parse(twitterResourceDetailed?.getBody())

    log.info "twitterId = ${twitterResponseDetailed['id']}"
    ...
}

You can get working example from my git repo. Grails Oauth Plugin Demo.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top