【问题标题】:Oauth2 PHP no data returnedOauth2 PHP没有返回数据
【发布时间】:2017-12-31 17:32:27
【问题描述】:

我在获取访问令牌时遇到了一些问题:

api.hybrid.ru/token

这个 API 的文档是俄语 (Link) ,它基本上说:

Headers
POST /token HTTP/1.1
Host: api.hybrid.ru
Content-Type: application/x-www-form-urlencoded

Data:
grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}

我有有效的 client_id 和 client_secret(hybrid.ru 技术人员给了我这些),它甚至不会返回错误消息。 我使用 CURL,我的代码如下:

<?php 

error_reporting(E_ALL);
ini_set('display_errors',1);

$ch = curl_init();

$url = "https://www.api.hybrid.ru/token";


$post_data = array(
'grant_type'=> 'client_credentials',
'client_id'=>'********',
'client_secret'=>'********'
);


curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"POST /token HTTP/1.1",
'Content-Type: application/x-www-form-urlencoded',
"host: api.hybrid.ru"
));

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data);

curl_setopt($ch, CURLOPT_POSTREDIR, 3);

$output = curl_exec($ch);

curl_close($ch);

print_r($output);

?>

我想,如果 client_id 或 client_secret API 有错误,至少应该返回一些错误消息。

还有一个问题,client_secret 可以包含字符“=”吗?他们给了我带有尾随“=”符号的client_secret。无论如何,无论有没有字符,它都不会返回任何数据。

有什么帮助吗?

更新 1

在遵循 Lorna Mitchell 的回答后,我收到以下消息:

Array ( 
[url] => https://api.hybrid.ru/Views/Errors/DefaultError.cshtml?aspxerrorpath=/ 
[content_type] => text/html; charset=utf-8 [http_code] => 500 [header_size] => 483 
[request_size] => 440 
[filetime] => -1 
[ssl_verify_result] => 0 
[redirect_count] => 1 
[total_time] => 3.853833 
[namelookup_time] => 0.972059 
[connect_time] => 1.178887 
[pretransfer_time] => 1.406303 
[size_upload] => 479 
[size_download] => 1763 
[speed_download] => 457 
[speed_upload] => 124 
[download_content_length] => 1763 
[upload_content_length] => 479 
[starttransfer_time] => 1.612792 [redirect_time] => 2.028607 
[redirect_url] => 
[primary_ip] => 212.8.236.42 
[certinfo] => Array ( ) 
[primary_port] => 443 
[local_ip] => 162.214.7.244 
[local_port] => 46088 
)

HTTP/1.1 100 Continue HTTP/1.1 
302 Found 
Date: Wed, 26 Jul 2017 16:47:47 GMT 
Content-Length: 166 
Connection: keep-alive 
Location: /Views/Errors/DefaultError.cshtml?aspxerrorpath=/ 
Server: Hybrid Web Server HTTP/1.1 100 Continue HTTP/1.1 500 Internal Server                        
Error 
Date: Wed, 26 Jul 2017 16:47:49 GMT 
Content-Type: text/html; charset=utf-8 
Content-Length: 1763 
Connection: keep-alive 
Cache-Control: private 
X-AspNet-Version: 4.0.30319 
Server: Hybrid Web Server

之后有什么想法吗?

【问题讨论】:

  • 您是否尝试过使用 telnet 发送帖子数据并查看是否收到任何响应?
  • 这是第一次听说 telnet,我遵循了 Lorna 的回答,我可能会通过电子邮件发送有关此 Hybrid 的信息,如果他们说问题在我这边,我会尝试您的方式,但不知道如何使用 telnet可以给我任何不同的答案,如果你能解释一下,那就太好了。谢谢你的回答。

标签: php rest curl oauth-2.0


【解决方案1】:

curl_error($ch) 是否返回任何有用的信息?

检查 CURLOPT_HTTPHEADER 的设置位置,并删除除内容类型之外的所有内容(此处的其他部分不是标头,文档中的示例显示了请求在发送时的外观)。

您还可以使用http://requestb.in 之类的工具检查您的代码发送的内容。去那里,创建一个新的 bin,并将其用作代码中的 URL。您将能够检查您的代码发送的内容并检查它是否正确。

如果看起来正确,那么您尝试集成的 API 存在错误 :(

【讨论】:

  • 感谢您的回答。它返回:“无法解析主机'www.api.hybrid.ru'”,在我从$url =“www.api.hybrid.ru/token”中删除“www”后,它显示“服务器错误'/' 应用”。这是否意味着 API 有错误?
  • 这听起来像是 API 的问题,肯定是一个错误
  • 确实是API有问题。
猜你喜欢
  • 2021-10-29
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
  • 2020-06-23
  • 1970-01-01
  • 2021-09-17
  • 1970-01-01
相关资源
最近更新 更多