【问题标题】:grails oauth from groovy script来自groovy脚本的grails oauth
【发布时间】:2012-02-10 06:46:00
【问题描述】:

我正在尝试运行一个调用 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 请求中使用的令牌无效”

谢谢。

【问题讨论】:

    标签: grails groovy oauth linkedin run-script


    【解决方案1】:

    看看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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      相关资源
      最近更新 更多