我分析了rdio頁面上的python示例與Scala所做的不同之處。
我覺得真的有兩個問題。
首先是您需要獲取訪問令牌。
第二個問題是顯然調度庫的符號方法使得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()
}
不熟悉斯卡拉,但我在Rdio工作。你是否將這些參數傳遞給POST請求的主體或查詢字符串中?請求的正文是正確的。還建議通過Authorization標頭傳遞OAuth憑證。 –
是的,「參數確實是這樣的:向POST主體添加參數。身份驗證標頭應該通過添加<@方法與密鑰 –
您使用哪個版本的調度? – alcarv