问题
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