我尝试通过PayPal实现登录。基本上,我只是复制了这个例子:How to Implement User Log-in with PayPal。 但是,我收到第二个响应的400 - 错误请求错误(在PayPal登录包含文件中)。我无法做出太多错误,也许有人知道怎么了错的。结果:
object(Httpful\Response)#14 (13) {
["body"]=>
string(0) ""
["raw_body"]=>
string(0) ""
["headers"]=>
object(Httpful\Response\Headers)#15 (1) {
["headers":"Httpful\Response\Headers":private]=>
array(10) {
["date"]=>
string(29) "Sun, 23 Jul 2017 13:30:12 GMT"
["server"]=>
string(6) "Apache"
["paypal-debug-id"]=>
string(13) "bcd5c7f1d86b2"
["www-authenticate"]=>
string(255) "Bearer error_description="GET /v1/oauth2/token/userinfo?schema=openidconnect returned a response status of 400 Bad Request",correlation_id="1283a4f54dc2a",error="400",information_link="https://developer.paypal.com/docs/api/#errors",realm="UserInfoService""
["set-cookie"]=>
string(52) "X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT"
["vary"]=>
string(29) "Accept-Encoding,Authorization"
["connection"]=>
string(5) "close"
["http_x_pp_az_locator"]=>
string(11) "sandbox.slc"
["transfer-encoding"]=>
string(7) "chunked"
["content-type"]=>
string(29) "text/html; charset=ISO-8859-1"
}
}
["raw_headers"]=>
string(983) "HTTP/1.1 400 Bad Request
Date: Sun, 23 Jul 2017 13:30:12 GMT
Server: Apache
Paypal-Debug-Id: 1283a4f54dc2a
WWW-Authenticate: Bearer error_description="GET /v1/oauth2/token/userinfo?schema=openidconnect returned a response status of 400 Bad Request",correlation_id="1283a4f54dc2a",error="400",information_link="https://developer.paypal.com/docs/api/#errors",realm="UserInfoService"
Set-Cookie: Apache=10.72.108.11.1500816612903307; path=/; expires=Tue, 16-Jul-47 13:30:12 GMT
Vary: Accept-Encoding,Authorization
Connection: close
HTTP_X_PP_AZ_LOCATOR: sandbox.slc
Paypal-Debug-Id: bcd5c7f1d86b2
Set-Cookie: X-PP-SILOVER=name%3DSANDBOX3.API.1%26silo_version%3D1880%26app%3Didentityspartaweb_api%26TIME%3D3835982937%26HTTP_X_PP_AZ_LOCATOR%3Dsandbox.slc; Expires=Sun, 23 Jul 2017 14:00:12 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT
Transfer-Encoding: chunked
Content-Type: text/html; charset=ISO-8859-1"
["request"]=>
object(Httpful\Request)#13 (22) {
["uri"]=>
string(80) "https://api.sandbox.paypal.com/v1/identity/openidconnect/userinfo/?schema=openid"
["method"]=>
string(3) "GET"
["headers"]=>
array(2) {
["Authorization"]=>
string(97) "A23AAHJ_6sujsm8hanJJWRQ8WqIjhbVvxqG-Z3g4Te3QzwkVdw6cLWgCeidGOgPng0kFx24dYlCoWhZNKlhTuDs-_knOqOERQ"
["Content-Length"]=>
int(0)
}
["raw_headers"]=>
string(493) "GET /v1/identity/openidconnect/userinfo/?schema=openid HTTP/1.1
Host: api.sandbox.paypal.com
Expect:
User-Agent: Httpful/0.2.19 (cURL/7.47.0 PHP/7.0.18-0ubuntu0.16.04.1 (Linux) nginx/1.10.0 Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0)
Content-Type: application/json
Accept: */*; q=0.5, text/plain; q=0.8, text/html;level=3;
Authorization: A23AAHJ_6sujsm8hanJJWRQ8WqIjhbVvxqG-Z3g4Te3QzwkVdw6cLWgCeidGOgPng0kFx24dYlCoWhZNKlhTuDs-_knOqOERQ
Content-Length: 0
"
["strict_ssl"]=>
bool(false)
["content_type"]=>
string(16) "application/json"
["expected_type"]=>
NULL
["additional_curl_opts"]=>
array(0) {
}
["auto_parse"]=>
bool(true)
["serialize_payload_method"]=>
int(2)
["username"]=>
string(80) "[I removed this.]"
["password"]=>
string(80) "[I removed this.]"
["serialized_payload"]=>
NULL
["payload"]=>
NULL
["parse_callback"]=>
NULL
["error_callback"]=>
NULL
["send_callback"]=>
NULL
["follow_redirects"]=>
bool(false)
["max_redirects"]=>
int(25)
["payload_serializers"]=>
array(0) {
}
["_ch"]=>
resource(4) of type (Unknown)
["_debug"]=>
NULL
}
["code"]=>
int(400)
["content_type"]=>
string(9) "text/html"
["parent_type"]=>
string(9) "text/html"
["charset"]=>
string(10) "ISO-8859-1"
["meta_data"]=>
array(26) {
["url"]=>
string(80) "https://api.sandbox.paypal.com/v1/identity/openidconnect/userinfo/?schema=openid"
["content_type"]=>
string(29) "text/html; charset=ISO-8859-1"
["http_code"]=>
int(400)
["header_size"]=>
int(987)
["request_size"]=>
int(486)
["filetime"]=>
int(-1)
["ssl_verify_result"]=>
int(0)
["redirect_count"]=>
int(0)
["total_time"]=>
float(0.919636)
["namelookup_time"]=>
float(5.1E-5)
["connect_time"]=>
float(0.193331)
["pretransfer_time"]=>
float(0.708154)
["size_upload"]=>
float(0)
["size_download"]=>
float(0)
["speed_download"]=>
float(0)
["speed_upload"]=>
float(0)
["download_content_length"]=>
float(-1)
["upload_content_length"]=>
float(-1)
["starttransfer_time"]=>
float(0.919562)
["redirect_time"]=>
float(0)
["redirect_url"]=>
string(0) ""
["primary_ip"]=>
string(11) "173.0.82.78"
["certinfo"]=>
array(0) {
}
["primary_port"]=>
int(443)
["local_ip"]=>
string(9) "10.0.2.15"
["local_port"]=>
int(50988)
}
["is_mime_vendor_specific"]=>
bool(false)
["is_mime_personal"]=>
bool(false)
["parsers":"Httpful\Response":private]=>
NULL
}
我的代码:( ppinit.php只定义常量)
require_once(dirname(__FILE__)."/ppinit.php");
$requestData = '?grant_type=authorization_code&code='.getGet("code").'&return_url=http://localhost/paypal/return';
$response = \Httpful\Request::get("https://" . PAYPAL_API_URL . "/v1/identity/openidconnect/tokenservice" . $requestData)
->authenticateWith(PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET)
->send();
$jsonResponse = json_decode($response->raw_body);
if(isset($jsonResponse->error))
{
echo "Fehler bei der Anmeldung mit PayPal.";
echo "<br/>";
exit;
}
//that's the one that does not work.
$response = \Httpful\Request::get("https://" . PAYPAL_API_URL . "/v1/identity/openidconnect/userinfo/?schema=openid")
->contentType("application/json")
->authorization($jsonResponse->access_token)
->authenticateWith(PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET)
->send();
$ppuser = json_decode($response);
答案 0 :(得分:1)
一般来说, HTTP 400错误请求意味着发送的请求格式错误,即客户端发送到服务器的数据流没有遵循规则 - read more
很难说出这些错误的确切来源,因为可能有很多,但我建议你将你的HTTP调用包装在try / catch块中,并追踪错误的原因。因此,在这种情况下,您可以用以下代码替换代码:
try {
$response = \Httpful\Request::get("https://" . PAYPAL_API_URL . "/v1/identity/openidconnect/tokenservice" . $requestData)
->authenticateWith(PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET)
->send();
} catch (Exception $e) {
var_dump($e->getMessage());
exit(1);
}
如果您正在使用命名空间,请在\
之前添加Exception
- &gt; \Exception
答案 1 :(得分:0)
使用下面的代码替换第二个请求。
try
{
$params = array('access_token' => $jsonResponse->access_token);
$userInfo = OpenIdUserinfo::getUserinfo($params, $paypal);
} catch (Exception $ex)
{
echo $ex;
exit(1);
}