grails scribe仅适用于twitter的身份验证

时间:2014-02-10 11:45:49

标签: twitter-bootstrap twitter grails-2.0 grails-plugin scribe

我不是再次写同样的问题,而是需要解决方案。其中询问如何在grails中为twitter进行仅应用程序身份验证。

下面是相同

的链接

How to implement application-only authentication for twitter in Grails using scribe?

Twitter application authentication to get user timelines for single user account

任何帮助都会受到极大的赞赏。

感谢

2 个答案:

答案 0 :(得分:3)

你可以做到这一点:

import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH

...

def requestResourceForTwitter(Token token, String url) {    
    String appAttribute = GrailsApplicationAttributes.APPLICATION_CONTEXT
    Object appContext = SCH.servletContext.getAttribute(appAttribute)
    def oauthService = appContext.oauthService
    Response response = oauthService.getTwitterResource(token, url)
    if(response.code == 200){
        return JSON.parse(response.body)
    }
}

答案 1 :(得分:1)

你需要做这样的事情,这对我有用。

 OAuthService service = new ServiceBuilder().provider(TwitterApi.class).apiKey(grailsApplication.config.oauth.providers.twitter.key).apiSecret(grailsApplication.config.oauth.providers.twitter.secret).build()
            OAuthRequest request = new OAuthRequest(Verb.GET, 'https://api.twitter.com/1.1/search/tweets.json?q=%23' + URLEncoder.encode(tag) + '&count=100&result_type=mixed&lang=en');
            Token accessToken = new Token(grailsApplication.config.oauth.providers.twitter.accessToken, grailsApplication.config.oauth.providers.twitter.accessSecret)
            service.signRequest(accessToken, request);
            Response response = request.send();
            JSONElement jsonMap = grails.converters.JSON.parse(response.getBody());