0

I am not writing the same question again but need the solution for it . Which queried that how to make application only authentication for twitter in grails.

below is the link for the same

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

Twitter application authentication to get user timelines for single user account

any help will be greatly admired .

thanks

Community
  • 1
  • 1
Jordon
  • 33
  • 6

2 Answers2

3

You can do it simply:

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)
    }
}
Dat Nguyen
  • 1,881
  • 17
  • 36
  • what about the steps shown by twitter to achieve application-only-auth here link. can you provide me example for more elaborate understanding of your solution ? – Jordon Feb 20 '14 at 11:18
1

you need to do something like this and this is working 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());
Shashank.gupta40
  • 915
  • 1
  • 8
  • 26
  • what about the steps shown by twitter to achieve application-only-auth here [link](https://dev.twitter.com/docs/auth/application-only-auth). can you provide me example for more elaborate understanding of your solution ? – Jordon Feb 11 '14 at 09:31