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

北战南征 提交于 2020-01-03 05:37:06

问题


Earlier i was using GET search/tweets of Twitter API 1.0 To get tweets according to #tags in Grails

Map jsonMap = grails.converters.JSON.parse(new URL('http://search.twitter.com/search.json?q=%23' + URLEncoder.encode(tag) + '&offset=' + offset + 'result_type=mixed&lang=en&page=' + page).text)

But due to change in Twitter API version 1.1 now the above call requires Authentication.

I want to fetch tweets on behalf of Application(Authentication) not by user Authentication.

Is this possible?

I came across application-only-auth but unable to implement it.

https://dev.twitter.com/docs/auth/application-only-auth

How to implement above using scribe API in Grails.


回答1:


I have done like this and it worked for me.

 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());


来源:https://stackoverflow.com/questions/18571324/how-to-implement-application-only-authentication-for-twitter-in-grails-using-scr

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