我正在尝试按照 docs 获取 Sage One API 的访问 token 使用 Guzzle (v6)/Laravel 5.2(Laravel 的参与与本题无关),卡在“请求访问 token ”阶段。
错误
Client error: `POST https://api.sageone.com/oauth2/token` resulted in a
`400 Bad Request` response: {"error":"unsupported_grant_type"}
违规代码
$client = new Client([
'base_uri'=>'https://api.sageone.com',
'headers' => ['content_type' => 'application/x-www-form-urlencoded']
]);
$data = [
'client_id' => getenv('SAGE_CLIENT'),
'client_secret' => getenv('SAGE_SECRET'),
'code' => $request->code,
'grant_type' => 'authorization_code',
'redirect_uri'=>'https://myurl.com/sage/refresh'
];
$response = $client->request('POST','/oauth2/token',['json' => $data]);
文档声明 “grant_type - 代码相关的授权类型。authorization_code 或 refresh_token。”,我都试过了。其他 var 都很好,只是 grant_type 似乎失败了。
更新 1 调试 header 生成以下输出。
* Hostname in DNS cache was stale, zapped * Trying 52.49.116.133...
* Connected to api.sageone.com (52.49.116.133) port 443 (#0)
* CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none
* ALPN/NPN, server did not agree to a protocol
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
* subject: CN=api.sageone.com,O=The Sage Group PLC,L=Newcastle Upon Tyne,ST=Newcastle Upon Tyne,C=GB,serialNumber=02231246,businessCategory=Private Organization,incorporationCountry=GB
* start date: May 12 00:00:00 2014 GMT
* expire date: May 11 23:59:59 2016 GMT
* common name: api.sageone.com
* issuer: CN=GeoTrust Extended Validation SSL CA - G2,O=GeoTrust Inc.,C=US > POST /oauth2/token HTTP/1.1 Host: api.sageone.com content_type: application/x-www-form-urlencoded User-Agent: GuzzleHttp/6.1.1 curl/7.43.0 PHP/5.6.18 Content-Type: application/x-www-form-urlencoded Content-Length: 216
* upload completely sent off: 216 out of 216 bytes < HTTP/1.1 400 Bad Request < Content-Type: application/json < Date: Tue, 08 Mar 2016 10:53:09 GMT < Server: openresty < Content-Length: 26 < Connection: keep-alive <
* Connection #0 to host api.sageone.com left intact
最佳答案
您将值作为 JSON 编码数据进行 POST,但您应该使用表单编码进行 POST。请参阅:http://docs.guzzlephp.org/en/latest/request-options.html#form-params , 所以
$response = $client->request('POST','/oauth2/token',['form_params' => $data]);
编辑:
您应该再次检查您的代码,因为我刚刚验证并仔细检查了此更改是否使其适用于 live sage 环境。我使用的完整代码:
<?php
require 'vendor/autoload.php';
$client = new GuzzleHttp\Client([
'base_uri'=>'https://api.sageone.com',
'headers' => ['content_type' => 'application/x-www-form-urlencoded']
]);
$data = [
'client_id' => getenv('SAGE_CLIENT'),
'client_secret' => getenv('SAGE_SECRET'),
'code' => getenv('SAGE_CODE'),
'grant_type' => 'authorization_code',
'redirect_uri'=>'https://myurl.com/sage/refresh'
];
$response = $client->request('POST','/oauth2/token',['form_params' => $data]);
echo $response->getBody();
?>
关于php - Sage One API - unsupported_grant_type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35861583/
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
在我的系统中,我已经定义了STI。Dog继承自Animal,在animals表中有一个type列,其值为"Dog"。现在我想让SpecialDog继承自dog,只是为了在某些特殊情况下稍微修改一下行为。数据还是一样。我需要通过SpecialDog运行的所有查询,以返回数据库中类型为Dog的值。我的问题是因为我有一个type列,rails将WHERE"animals"."type"IN('SpecialDog')附加到我的查询中,所以我不能获取原始的Dog条目。所以我想要的是以某种方式覆盖rails在通过SpecialDog访问数据库时使用的值,使其表现得像Dog。有没有办法覆盖用于类型
我正在为一个类赋值,它在rspec测试中使用了column_types方法。it"Userdatabasestructureinplace"doexpect(User.column_names).toinclude"password_digest","username"expect(User.column_types["username"].type).toeq:stringexpect(User.column_types["password_digest"].type).toeq:stringexpect(User.column_types["created_at"].type).t
如何将自己的字段类型添加到formtastic中?例如,我需要一个自定义的日期时间输入,我想要这样的东西::my_date%>这显然是行不通的,因为formtastic不知道:my_date(只有:boolean、:string、:datetime等等...)但是我怎样才能添加额外的输入类型呢? 最佳答案 您需要添加自定义输入法:classMyCustomFormtasticFormBuilder这非常适合新的HTML5输入类型。你可以这样使用它:MyCustomFormtasticFormBuilderdo|f|%>:my_dat
希望我没有误解“ducktyping”的含义,但从我读到的内容来看,这意味着我应该根据对象如何响应方法而不是它是什么类型/类来编写代码。代码如下:defconvert_hash(hash)ifhash.keys.all?{|k|k.is_a?(Integer)}returnhashelsifhash.keys.all?{|k|k.is_a?(Property)}new_hash={}hash.each_pair{|k,v|new_hash[k.id]=v}returnnew_hashelseraise"CustomattributekeysshouldbeID'sorPropertyo
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它
我在rails中有一个API端点,默认情况下,如果您没有设置任何Content-Typeheader,则会处理中的参数application/x-www-form-urlencoded有没有办法在不指定header中的内容类型的情况下处理来自POST请求的rails中的json字符串? 最佳答案 在您的routes.rb文件中,您可以将POST路由放置在命名空间中,并像这样定义预期的格式:namespace:api,defaults:{format::json}dopost'example'=>'controller#action'
我有以下代码:classProfileLookup基本上包含大量查找数据,按类别拆分。目的是为数据库中的每个类别创建一个方法。通过Rails控制台,此代码按预期工作:ruby-1.9.3@hub:002>ProfileLookup.available_gendersProfileLookupLoad(0.6ms)SELECT"profile_lookups".*FROM"profile_lookups"WHERE"profile_lookups"."category"='gender'ORDERBYvalue=>["Female","Male"]但是,我的规范不合格。以下规范:requ
我需要定义用于序列化/反序列化对象的自定义方法。我想做类似下面的事情。classPersondefto_yaml_type"!example.com,2010-11-30/Person"enddefto_yaml"stringrepresentingperson"enddeffrom_yaml(yaml)Person.load_from(yaml)endend声明序列化/反序列化的正确方法是什么? 最佳答案 好的,这就是我想出的classPersondefto_yaml_type"!example.com,2010-11-30/pe
require'net/http'require'rubygems'require'json'url=URI.parse('http://www.xyxx/abc/pqr')resp=Net::HTTP.get_response(url)#get_responsetakesanURIobjectdata=resp.bodyputsdata这是我在ruby中的代码,resp.data以xml形式提供给我数据。restapi默认返回xml数据,如果headercontent-type是application/json,则返回json。但我想要json格式的数据。为此我必须设置heade