简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM