2016-11-29 13 views
3

Salesforce 내에서 Google OAuth를 사용할 때 400 건의 잘못된 요청을 받았습니다. 다음은 잘못된 grant_type과 관련된 오류입니다.하지만 '새로 고침 토큰 사용'아래의 설명서를 보면 올바르게 표시됩니다.Google OAuth2.0을 사용할 때 잘못된 요청이 발생했습니다.

https://developers.google.com/identity/protocols/OAuth2WebServer

오류 :

{ 
"error": "unsupported_grant_type", 
"error_description": "Invalid grant_type: " 
} 

내가 액세스 토큰에 대한 refresh_token도 교환을 시도하고 성공적으로는 다음 코드를 사용하여, CURL을 사용 할 수 있습니다.

curl \ 
    -d refresh_token=REFRESH_TOKEN \ 
    -d client_id=CLIENT_ID \ 
    -d client_secret=CLIENT_SECRET \ 
    -d grant_type=refresh_token https://www.googleapis.com/oauth2/v4/token 

내가 세일즈 포스 내에서 사용하고 코드 :

Http http = new Http(); 
    HttpRequest req = new HttpRequest(); 

    req.setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 
    req.setHeader('Content-Length', '0'); 
    req.setHeader('client_id', 'CLIENT_ID'); 
    req.setHeader('client_secret', 'CLIENT_SECRET'); 
    req.setHeader('refresh_token', 'REFRESH_TOKEN'); 
    req.setHeader('grant_type', 'refresh_token'); 

    req.setEndpoint('https://www.googleapis.com/oauth2/v4/token'); 
    req.setMethod('POST'); 

    return http.send(req); 

답변

5

-d 컬 옵션은 이러한 매개 변수를 전송하는 지원되는 방법 중 하나입니다 application/x-www-form-urlencoded 콘텐츠 형식을 사용하여 요청 본문에 데이터를 전송 OAuth2에서 세일즈 포스의 코드에서

-d Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.

는 올바른 콘텐츠 형식을 설정하는,하지만 요청 본문에서 그들을 보내는 대신 추가 헤더로 OAuth2를 관련 매개 변수를 보내고있다.

application/x-www-form-urlencoded 인코딩을 사용하여 요청 본문에 매개 변수를 보내려면 코드를 업데이트해야합니다.

+0

req.setHeader ('Content-Type', 'application/x-www-form-urlencoded; charset = UTF-8'); 'charset = UTF-8'을 제거하여 위의 행을 수정했지만 위에 나열된 것과 같은 오류가 계속 나타납니다. –

+0

변경해야 할 것은'client_id','grant_type' 등과 같은 OAuth 매개 변수를 전달하는 방법입니다. 그들은 setHeader를 사용할 수없고 대신 POST 요청 본문에 전달됩니다. –

+0

당신이 옳았습니다. –

관련 문제