Grails: Oauth Twitter userID from token

戏子无情 提交于 2019-12-25 09:40:19

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/19866700/grails-oauth-twitter-userid-from-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!