2016-11-29 6 views
3

Salesforce内からGoogle OAuthを使用しているときに400回の不正なリクエストを受け取りました。次のエラーは無効なgrant_typeですが、「Refresh Tokenの使用」のドキュメントを見ると、正しいことがわかります。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 

私はSalesforceの内部で使用していますコード:

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でSalesforceのコードで

-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

あなたは正しいです。 –

関連する問題