0

我正在尝试运行一个调用 Linkedin API 的 groovy 脚本。问题是,如何使用 groovy 脚本中的 grails oauth 插件进行身份验证?这是我的配置:

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"
    }     
}   

这是我的脚本,我以 "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"

如果我打印响应,我会得到“OAuth 请求中使用的令牌无效”

谢谢。

4

1 回答 1

0

看看http://code.google.com/p/oauth-signpost/wiki/TwitterAndSignpost

我下载了 OAuthTwitterExample 并将压缩包中包含的 commons-codec-1.3.jar 和 signpost-core-1.1-SNAPSHOT.jar 放在我的工作目录中

对于 flickr OAuth,我在 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

在工作目录的命令行中输入

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

希望这可以帮助

于 2012-12-29T03:32:56.900 回答