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.