gpt4 book ai didi

web-services - 在Scala中使用调度访问Rdio oauth API时出错

转载 作者:行者123 更新时间:2023-12-01 05:13:29 24 4
gpt4 key购买 nike

我正在尝试使用 Scala 调度来访问 Rdio API,如下所示:

import dispatch.url
import dispatch.Http
import dispatch.Defaults.executor
import dispatch._
import com.ning.http.client.oauth.ConsumerKey
import dispatch.oauth._

val consumer = new ConsumerKey("my key", "my secret")
val params = Map("method" -> "get", "keys" -> id, "extras" -> "-*,playCount")
val request = url("http://api.rdio.com/1/").POST <@ consumer << params <:< Map("Accept" -> "application/json")
val response = Http(request OK as.String)

我收到错误 403。

怎么了?我确定我的 key 是正确的。

最佳答案

我已经分析了 rdio 页面上的 python 示例与 Scala 所做的区别。

我认为真的有两个问题。

  • 首先是您需要获取访问 token 。
  • 第二个问题,显然dispatch library的sign方法让rdio不爽。它删除尾部斜杠,这使得签名不匹配。

  • 第一个问题很容易解决——你只需要使用 Exchange 类,它会为你完成大部分工作。

    第二个问题比较棘手,我刚刚复制了原版 sign方法并更改了删除尾部斜杠的部分。

    代码如下。
    val ck = new ConsumerKey("YOUR_CONSUMER_KEY", "YOUR_CONSUMER_TOKEN")

    // declare exchange to obtain an access token
    val exchange = new Exchange with SomeHttp with SomeCallback with SomeConsumer with SomeEndpoints {

    override def http: HttpExecutor = Http

    override def callback: String = "oob"

    override def consumer: ConsumerKey = ck

    override def accessToken: String = "http://api.rdio.com/oauth/access_token"

    override def authorize: String = "http://api.rdio.com/oauth/authorize"

    override def requestToken: String = "http://api.rdio.com/oauth/request_token"
    }

    /// we change the default method of the dispatch
    def sign(request: Req, consumer: ConsumerKey, token: RequestToken): Req = {
    val calc = new OAuthSignatureCalculator(consumer, token)
    request underlying { r =>
    val req = r.build
    //!!! here we make change so the trailing slash is not removed
    val baseurl = req.getURI.toString.takeWhile(_ != '?').mkString("")
    calc.calculateAndAddSignature(baseurl, req, r)
    r
    }
    }

    val response = exchange.fetchRequestToken.right.flatMap { rt =>

    // point your browser to this URL with the given oauth token, we'll get PIN back
    println(s"Go to https://www.rdio.com/oauth/authorize/?oauth_callback=oob&oauth_token=${rt.getKey}")
    print("Enter PIN:")
    val pin = readLine()

    exchange.fetchAccessToken(rt, pin)
    }.right.flatMap { at =>
    // now we can call API using the consumer key and the access token
    val request = sign(url("http://api.rdio.com/1/").POST << Map("method" -> "currentUser"), ck, at)
    val response = Http(request > as.String)
    response.map(Right(_))
    }

    response.map { responseOrError =>
    responseOrError.fold(err => println(s"Error $err"), suc => println(s"Response: $suc"))
    Http.shutdown()
    }

    关于web-services - 在Scala中使用调度访问Rdio oauth API时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22674407/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com