grails oauth from groovy script

1k views Asked by At

I'm trying to run a groovy script that calls the Linkedin API. The question is, how do I authenticate using the grails oauth plugin from within the groovy script ? This is my config :

oauth {
    linkedin {
        requestTokenUrl="https://api.linkedin.com/uas/oauth/requestToken"
        accessTokenUrl="https://api.linkedin.com/uas/oauth/accessToken"
        authUrl="https://www.linkedin.com/uas/oauth/authenticate"
        consumer.key="xxxxxxxxx"
        consumer.secret="xxxxxxxxxx"
    }     
}   

This is my script, which I start with "grails run-script scriptname.groovy" :

import org.grails.plugins.oauth.OauthService
def oauthService = new OauthService()
oauthService.reset()
def URL = "http://api.linkedin.com/v1/people-search?country-code=us&postal-code=98102&distance=100&start=0&count=5"
def recs_response = oauthService.accessResource( URL, "linkedin", [key:"xxxxxxx", secret:"xxxxxxxxx"], 'GET')

println "it worked"

If I print the response I get "The token used in the OAuth request is not valid"

Thanks.

1

There are 1 answers

0
javazquez On

Take a look at http://code.google.com/p/oauth-signpost/wiki/TwitterAndSignpost

I downloaded the OAuthTwitterExample and put the commons-codec-1.3.jar and signpost-core-1.1-SNAPSHOT.jar that were included in the zip in my working directory

for flickr OAuth I used the following in oauth.groovy

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.signature.SignatureMethod;

def consumer = new DefaultOAuthConsumer('<API KEY>','<Signature>',  
SignatureMethod.HMAC_SHA1)

def provider = new  
DefaultOAuthProvider(consumer,"http://www.flickr.com/services/oauth/request_token",                                  
"http://www.flickr.com/services/oauth/access_token",                                         
"http://www.flickr.com/services/oauth/authorize");

String url =provider.retrieveRequestToken( OAuth.OUT_OF_BAND);
println "navigate to the following URL"
println url

enter this on commandline in the working directory

groovy -cp commons-codec-1.3.jar:signpost-core-1.1-SNAPSHOT.jar oauth.groovy

hope this helps