2016-07-19 2 views
-1

이 내가 생각PHP POST 요청 및 unsupported_grant_type

$data = array(
     'grant_type' => 'Password', 
     'username' => 'username', 
     'password' => 'pwd', 
     'DeviceId' => '' 
    ); 


    $url = "http://test/sync/oauth/token";  

    $curl = curl_init($url); 
    curl_setopt($curl, CURLOPT_HEADER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 

    $response = curl_exec($curl); 

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); 

    if ($status != 201) { 
     die("Error: call to URL $url failed with status $status, response $response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl)); 
    } 


    curl_close($curl); 

    print_r($response);` 

가 올바른지 내 코드이지만, 서버는 나에게이 오류

Error: call to URL http://test/sync/oauth/token failed with status 400, response {"error":"unsupported_grant_type"}, curl_error , curl_errno 0

오류가를 반환?

+0

컬 년대 잘 작동 : 그것을 필요로하는 사람들을 위해

$headers = array(); $headers[] = 'Content-Type: application/x-www-form-urlencoded'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
CD001

+0

예, 올바르지 만 오류는 남아 있습니다. 오류 : URL http://test.audipsp.volkswagengroup.it/sync/oauth/token으로 전화하면 상태 400, 응답 HTTP/1.1로 실패합니다. 계속 예 맞아,하지만 오류가 남아 : HTTP/1.1 400 잘못된 요청 캐시 제어 : 노 캐시 에서 Pragma : 노 캐시 콘텐츠 길이 : 34 콘텐츠 형식 : 응용 프로그램/JSON; 문자셋 = UTF- 8 만료 : -1 서버 : Microsoft-IIS/8.5 X-Powered-By : ASP.NET 날짜 : 2016 년 7 월 19 일 10:29:27 GMT 오류가 조금 변경되었지만 unsupported_grant_type이 동일 함 : '오류 : URL http : // test/sync/oauth/token으로 실패 함 : failed with : "오류 :"unsupported_grant_type "}, curl_error, curl_errno 0 –

답변

0

OAuthAuthorizationServerHandler의 기본 구현은 유일한 형태의 인코딩 (예 : 응용 프로그램/x-www-form-urlencoded를)하지 JSON 인코딩 (응용 프로그램/JSON)

요청의 ContentType이 있어야한다 응용 프로그램/x 축을 받아 www-form-urlencoded 다음과 같이 본문 데이터를 전달하십시오. grant_type = password & username = username & password = password 즉 JSON 형식이 아닙니다. // 테스트/동기화/OAuth를 : - 오류가 JSON에 http`에 의해 생성 된 것으로 보인다

$data = array(
    'grant_type' => 'password', 
    'username' => 'username', 
    'password' => 'password', 
    'DeviceId' => '' 
); 
$url_token = "http://test/sync/oauth/token";  
try { 
    $oauth = new OAuth('username','password'); 
    $signature = $oauth->fetch ($url_token,$data,'POST'); 
    $response_info = $oauth->getLastResponseInfo(); 
    header("Content-Type: {$response_info["content_type"]}"); 
    $token = $oauth->getLastResponse(); 
    $token = json_decode($token, true); 

    $authorization_token = $token['access_token']; 
} catch(OAuthException $E) { 
    echo "Response: ". $E->lastResponse . "\n"; 
} 
    return $authorization_token; 
+0

상태 (400), 응답 HTTP/1.1 100 계속 HTTP/1.1 400 잘못된 요청 캐시 제어 : 노 캐시 에서 Pragma : 노 캐시 콘텐츠 길이 : 34 콘텐츠 형식 : 응용 프로그램/JSON; 문자셋 = UTF- 8 이 만료 : -1 서버 : 마이크로 소프트 IIS/8.5 X-구동 - 기준 : ASP.NET 날짜 : 2016년 7월 19일 (화) 그리니치 표준시 10시 29분 27초 { "오류": "unsupported_grant_type"}, curl_error , curl_errno 0 ' –

+0

요청 헤더를 보여줄 수 있습니까? 요청 헤더의 내용 유형을 확인해야합니다. 또는 dev 도구를 사용하여 Chrome에서 http://test.audipsp.volkswagengroup.it/sync/oauth/token?grant_type=password&username=username&password=password를 시도해보십시오. – DarkKnight

+0

요청 헤더에는 매개 변수가 필요하지 않으므로 올바른 내용 유형을 알지 못합니다. –

0

... 이 해결책이다/token` ... 나는 추측을 위험하게 할 것이고`grant_type`이 * password * (소문자 'p'를 가짐)이 될 것이라고 기대하지만, 그 스크립트의 모든 소스를 보지 않고, 짐작.