首页 文章

使用Ruby的HTTParty的RingCentral OAuth服务器密码流

提问于
浏览
2

我'm currently trying to write a Data Extraction Application for RingCentral to dump CSVs to AWS as part of a Data Warehousing project. I'已经审阅了他们的文档的几个部分,概述了如何使用密码流进行身份验证,但似乎有一些基于文档here中的示例缺少的部分 .

在“密码流”下列出的示例中有一个从未提及的身份验证标头,并且包含的值由作者凭空掏出,然后在下面的身份验证步骤中从未提及过 . 我可以通过范围“读取呼叫日志”验证我的应用程序是否满足专用,仅服务器要求 . 下面是我的设置的屏幕截图:[链接到图像,因为我没有足够的声誉......呃] [2]

使用Sandbox凭据和 endpoints 进行开发,这是我到目前为止所尝试的内容:

url = 'https://platform.devtest.ringcentral.com/restapi/oauth/token'
headers = {'Content-Type'=>'application/x-www-form-urlencoded'}
body = {:grant_type=>"password", :username=>"13133982093", :password=><OUR_SANDBOX_PASSWORD>}
resp = HTTParty.post(url, headers: headers, body: URI.encode_www_form(body))

印刷的答复:

#<HTTParty::Response:0x7ff29588a420 parsed_response={"error"=>"invalid_client", "error_description"=>"Invalid client: ", "errors"=>[{"errorCode"=>"OAU-153", "message"=>"Invalid client: ", "parameters"=>[{"parameterName"=>"client_id", "parameterValue"=>""}]}]}, @response=#<Net::HTTPBadRequest 400 Bad Request readbody=true>, @headers={"server"=>["nginx/1.10.2"], "date"=>["Tue, 20 Jun 2017 22:05:36 GMT"], "content-type"=>["application/json;charset=utf-8"], "transfer-encoding"=>["chunked"], "connection"=>["close"], "x-application-context"=>["application:8080"], "content-language"=>["en"], "www-authenticate"=>["Bearer realm=\"RingCentral REST API\", error=\"invalid_client\", error_description=\"Invalid client: \""], "rcrequestid"=>["96aaacb0-5604-11e7-97a9-005056bb594d"], "aceroutingkey"=>["sjc11-c01-ace01.c83d65c2-46d3-11e7-ab8e-005056a73f60"], "x-server-name"=>["sjc06-c01-hlb02"], "x-request-time"=>["0.010"], "x-upstream-server"=>["10.24.22.193:8080"], "x-upstream-status"=>["400"], "x-upstream-htime"=>["0.010"], "x-upstream-rtime"=>["1497996336.573"], "x-upstream-ctime"=>["0.000"], "x-tcpinfo"=>["1000, 500, 10, 28960"], "routingkey"=>["SJC11P01"]}>

我've tried Google Searching for the Error Code OAU-153 and it'完全没有记录 . 我甚至包括像client_id和client_secret(a.k.a - app_key和app_secret)生成的Authorization Header之类的东西: auth= {'Authorization': "Basic #{ Base64.encode64("#{client_id}:#{client_secret}") }"}

其中包括如下电话: resp = HTTParty.post(token_url, headers: headers, body: URI.encode_www_form(token_body), auth: auth)

但是这仍然产生与上面相同的错误,错误描述 "Invalid Client: " 和error_code OAU-153 .

此外,由于项目要求,Ring Central Ruby SDK会导致与当前应用程序中非常重要的gems的依赖冲突,因此我们不得不为此数据源编写新的Auth代码 .

在这一点上,我大量验证了我的密钥,用户名和密码都是正确的 . 我已将 username 电话号码值包含在格式化检查中 . 我基本上留下了两个我无法回答的问题:

1)我正确地接受了这个吗?我是否为此应用程序选择了正确的角色和权限?我是否需要使用不同的Auth Flow和Privacy设置创建新的?

2)我是否使用了正确的工具? HTTParty可能是它如何发送请求的罪魁祸首吗?在我花费的时间里,在资源库中挖掘并没有引起太多关注 .

编辑:

我们对项目进行了更改,允许我们使用SDK进行身份验证,如下所示:

client = RingCentralSdk::REST::Client.new do | config |
    config.app_key = ENV['RC_CLIENT_ID']
    config.app_secret = ENV['RC_CLIENT_SECRET']
    config.server_url = @server_url
    config.username = @account_num
    config.password = @password
    config.extension = @extension_id

然后我们将令牌信息存储为哈希: token_hash = client.token.to_hash

哈希的一个例子如下:

{"token_type"=>"bearer", "refresh_token_expires_in"=>604800, 
 "scope"=>"ReadCallLog", "owner_id"=>[COMPANY's OWNER_ID], 
 "endpoint_id"=>"N0UV835BQ2C_Bz2ofJA1Eg", 
 :access_token=> [REMOVED FOR SECURITY],
 :refresh_token=> [REMOVED FOR SECURITY], 
 :expires_at=>1498081681}

但是,现在我无法执行2个操作:简单的经过身份验证的请求或令牌刷新 .

尝试简单的呼叫记录请求:

uri = "https://platform.devtest.ringcentral.com/restapi/v1.0/account/<phone_num_with_pre_+>/extension/101/call-log"
headers = {"Accept"=>"application/json", "Authorization"=> "#{ token_hash[:access_token] }"} 
resp = HTTParty.get(uri, headers: headers)

这种请求给我一个错误:

#<HTTParty::Response:0x7fdf2b0ae800 parsed_response={"errorCode"=>"InvalidParameter", "message"=>"Resource for parameter [accountId] is not found", "errors"=>[{"errorCode"=>"CMN-102", "message"=>"Resource for parameter [accountId] is not found", "parameterName"=>"accountId"}], "parameterName"=>"accountId"}, @response=#<Net::HTTPNotFound 404 Not Found readbody=true>, @headers={"server"=>["nginx/1.10.2"], "date"=>["Wed, 21 Jun 2017 21:39:58 GMT"], "content-type"=>["application/json;charset=UTF-8"], "content-length"=>["290"], "connection"=>["close"], "rcrequestid"=>["2b66c9d6-56ca-11e7-b418-005056bb26b9"], "routingkey"=>["SJC11P01PAS02"], "x-error-id"=>["2b66c9d6-56ca-11e7-b418-005056bb26b9"], "content-language"=>["en-US"], "x-rate-limit-group"=>["heavy"], "x-rate-limit-limit"=>["10"], "x-rate-limit-remaining"=>["9"], "x-rate-limit-window"=>["60"]}>

此错误表明有两件事,1)我的account_id参数输入错误 - 但是,我直接从RC Developer Portal为我的应用程序复制了Sandbox编号,两者都带有前置的 + 和没有,并且发生了同样的错误,2)没有 call-log 记录,因此API服务器认为资源不存在并抛出错误 .

所以,我想我的问题是?当没有数据供我查询时,我怎么能对这个Sandbox进行开发?有没有简单的方法来为这个帐户制作数据?

尝试刷新令牌流程:

注意:我每次需要令牌时都会再次登录 . 笨重但不可怕 .

执行请求:

uri = "https://platform.devtest.ringcentral.com/restapi/oauth/token"
refresh_headers = {"Accept"=>"application/json", "Content-Type"=>"application/x-www-form-urlencoded", 
                   "Authentication"=>"Basic [BASE64_ENCODED_APP_KEY:APP_SECRET_DELIMITED_COMBO]}
refresh_body = {:grant_type=>"refresh_token", :refresh_token=>"#{token_hash[:refresh_token]}"} 
resp = HTTParty.post(uri, headers: refresh_headers, body: URI.www_encode_form(refresh_body))

获得错误:

#<HTTParty::Response:0x7fdf3055d8d8 parsed_response={"error"=>"invalid_client", "error_description"=>"Invalid client: ", "errors"=>[{"errorCode"=>"OAU-153", "message"=>"Invalid client: ", "parameters"=>[{"parameterName"=>"client_id", "parameterValue"=>""}]}]}, @response=#<Net::HTTPBadRequest 400 Bad Request readbody=true>, @headers={"server"=>["nginx/1.10.2"], "date"=>["Wed, 21 Jun 2017 22:16:07 GMT"], "content-type"=>["application/json;charset=utf-8"], "transfer-encoding"=>["chunked"], "connection"=>["close"], "x-application-context"=>["application:8080"], "content-language"=>["en"], "www-authenticate"=>["Bearer realm=\"RingCentral REST API\", error=\"invalid_client\", error_description=\"Invalid client: \""], "rcrequestid"=>["3958edb2-56cf-11e7-8758-005056bb26b9"], "aceroutingkey"=>["sjc11-c01-ace01.c83d65c2-46d3-11e7-ab8e-005056a73f60"], "x-server-name"=>["sjc06-c01-hlb01"], "x-request-time"=>["0.000"], "x-upstream-server"=>["10.24.22.193:8080"], "x-upstream-status"=>["400"], "x-upstream-htime"=>["0.000"], "x-upstream-rtime"=>["1498083367.846"], "x-upstream-ctime"=>["0.000"], "x-tcpinfo"=>["1000, 500, 10, 28960"], "routingkey"=>["SJC11P01"]}>

默默地对自己哭泣......

:'-(

令人沮丧的是,RingCentral Ruby SDK的文档没有用于刷新这些令牌的文档化方法,并且模糊地提到了“手动刷新”的内容 . 对我的请求或SDK的任何想法或想法将不胜感激 . 我宁愿一次又一次地进行刷新而不是重新认证 .

1 回答

  • 0

    令人沮丧的是,RingCentral Ruby SDK的文档没有用于刷新这些令牌的文档化方法,并且模糊地提到了“手动刷新”的内容 . 对我的请求或SDK的任何想法或想法将不胜感激 . 我宁愿一次又一次地进行刷新而不是重新认证 .

    令牌刷新位于repo的 /docs 文件夹中的详细文档中,并且也发布在Ruby SDK's Read the Docs上 . 我希望这些文档更加明显 .

    文档的令牌刷新部分可在此处获得:

    http://ringcentral-sdk-ruby.readthedocs.io/en/latest/usage/authorization/Authorization/#token-refresh

    关于通话记录请求,您可以使用下面的API调用直接使用SDK .

    client = RingCentralSdk::REST::Client.new do | config |
      # ...
    end
    
    client.http.get 'account/~/extension/~/call-log'
    

    对于 accountIdextensionId 路径参数,您可以使用 ~ 指示当前登录用户的帐户和分机 . 如果要指定 accountIdextensionId ,请注意这些是后端标识值并且与用户拨打的公司主电话号码或 extensionNumber 不对应 . 您可以通过调用 account/~/extension API endpoints 为您的登录用户检索 accountIdextensionId .

相关问题