2

我正在使用官方 SoundCloud API Wrapper for Ruby(soundcloud来自https://github.com/soundcloud/soundcloud-ruby)来访问 SoundCloud API。Oauth 正在使用中并已正确设置。我能够访问/me/activities和其他/me/...资源。成功上传声音、查询曲目、评论等。

但是,当我尝试使用以下 API 调用访问特定的关注者(通过 id)时,我收到 401 Unauthorized 错误:

api_response = @soundcloud.get("/me/followings/#{params[:id]}")

查询以下列表工作正常:

api_response = @soundcloud.get("/me/followings")

关于为什么会发生这种情况的任何见解?我假设 soundcloud gem 将 oauth_token/client_id 适当地附加到 API 调用,就像它对其他资源一样。

加法

https://github.com/soundcloud/soundcloud-ruby的克隆中,可以通过以下RSpec测试重现:

require 'helper'

RSpec.configure do |config|
  WebMock.allow_net_connect!
end

describe SoundCloud do
  context 'initialized with access_token' do
    subject {
      SoundCloud.new(:access_token => ENV['ACCESS_TOKEN'],
                     :client_id => ENV['CLIENT_ID'],
                     :client_secret => ENV['CLIENT_SECRET'])}

    describe "#get /me" do
      it "returns authenticated user details" do
        resp = subject.send(:get, '/me')
        # pp resp.inspect
        expect(resp.kind).to eq('user')
      end
    end
    describe "#get /me/followings" do
      it "returns authenticated user followings" do
        resp = subject.send(:get, '/me/followings')
        # pp resp.inspect
        expect(resp).to be_an Array
        expect(resp[0].kind).to eq('user')
      end
    end
    describe "#get /me/followings/#{ENV['TEST_ID']}" do
      it "returns authenticated user following for id #{ENV['TEST_ID']}" do
        resp = subject.send(:get, "/me/followings/#{ENV['TEST_ID']}")
        # pp resp.inspect
        expect(resp.kind).to eq('user')
      end
    end
  end
end

使用以下脚本运行测试:

#!/bin/bash

export CLIENT_ID='Your Client ID'
export CLIENT_SECRET='Your Client Secret'
export ACCESS_TOKEN='Your Access Token'
export TEST_ID='Id of someone you follow'

echo CLIENT_ID: $CLIENT_ID
echo CLIENT_SECRET: $CLIENT_SECRET
echo ACCESS_TOKEN: $ACCESS_TOKEN
echo TEST_ID: $TEST_ID

echo '------'

ruby -S rspec spec/followings_spec.rb

测试运行后,最后一个测试“#get /me/followings/#{ENV['TEST_ID']}”将失败,但它不应该失败 - 除非我的期望是错误的。

4

0 回答 0