其grails项目,
Twitter身份验证成功,
如何从这个oauth_token获得twitter userID?
感谢
答案 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