문제 설명
수명이 긴 토큰을 얻는 동안 신뢰할 수 없는 결과 (Unreliable results while obtaining long‑lived token)
여기에서 액세스 토큰에 대한 FB 지침을 따르고 있습니다. https:// developer.facebook.com/docs/facebook‑login/access‑tokens
그들은 내가 한 Javascript SDK를 사용하여 클라이언트로부터 단기 토큰을 얻으라고 말합니다. 테스트하는 동안 매번 문제 없이 유효한 단기 토큰을 얻습니다.
그런 다음 서버에서 다음과 같은 구조의 URL에 대해 cURL 요청을 만듭니다.
https://graph.facebook.com/v4.0/oauth/access_token?grant_type=fb_exchange_token&fb_exchange_token=[short‑lived‑token‑here]&client_id=[client‑ID‑here]&client_secret=[client‑secret‑here]
이것은 작동합니다. 시간의 약 10% ‑ 30%이고 다음과 같은 성공적인 응답을 얻습니다.
{"access_token":"[LONG‑TERM‑TOKEN‑HERE]","token_type":"bearer"}
그러나 시간의 70% ‑ 90%에서 다음과 같은 오류가 수신됩니다.
OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to graph.facebook.com:443
작동할 때와 작동하지 않을 때 완전히 무작위입니다. 내 cURL 코드는 다음과 같습니다.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Authority: graph.facebook.com';
$headers[] = 'Pragma: no‑cache';
$headers[] = 'Cache‑Control: no‑cache';
$headers[] = 'Upgrade‑Insecure‑Requests: 1';
$headers[] = 'User‑Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36';
$headers[] = 'Sec‑Fetch‑User: ?1';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed‑exchange;v=b3;q=0.9';
$headers[] = 'Sec‑Fetch‑Site: none';
$headers[] = 'Sec‑Fetch‑Mode: navigate';
$headers[] = 'Accept‑Encoding: gzip, deflate, br';
$headers[] = 'Accept‑Language: en‑US,en;q=0.9';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
URL로 이동할 때 내 브라우저에서 성공적으로 호출되었기 때문에 이것을 사용했습니다. 그러나 불필요한 헤더도 제거하려고 시도했고 똑같이 예측할 수 없는 결과를 얻었습니다.
내 앱 할당량의 <1%에 있기 때문에 요청 제한과 아무 관련이 없는 것 같습니다. 나는 또한 cURL 요청 사이에 >10초를 기다리려고 시도했지만 결과에 영향을 미치지 않았습니다.
이상한 점은 내가 이것을 수행할 때마다 FB가 나에게 유효한 액세스 코드를 제공하는 것 같다는 것입니다. 실제 웹 브라우저에서 요청합니다. 이 문제는 내 PHP cURL 요청이 내 브라우저의 XHR 요청과 동일하더라도 cURL을 사용할 때만 존재합니다.
참조 솔루션
방법 1:
This is less of an answer and more of a workaround. I tried many different configurations with cURL ‑ none of them were reliable.
However I found one simple method to work 100% of the time, which was to use file_get_contents()
.
(by Wills Manley、Wills Manley)