Grails:来自令牌的Oauth Twitter userID

时间:2013-11-08 19:07:19

标签: grails oauth oauth-2.0 twitter-oauth scribe

其grails项目,
Twitter身份验证成功,
如何从这个oauth_token获得twitter userID?
感谢

1 个答案:

答案 0 :(得分:3)

试试这个..,。

<强>配置:

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']}"
    ...
}

你可以从我的git repo获得工作示例。 Grails Oauth Plugin Demo