RestSharp Google 的 OAuth

RestSharp Google's OAuth

如果我在 Google 的 OAuth 服务器上将 RestSharp 用于 grant_type=authorization_code,我会收到以下响应:

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unexpected token.\ngrant_type=authoriza\n^",
    "status": "INVALID_ARGUMENT"
  }
}

如果我用 Fiddler 发出请求,一切正常,它也可以与其他 OAuth 服务器一起工作。

休息请求:

RestClient  restClient = new RestClient(@"https://www.googleapis.com/");
RestRequest restRequest = new RestRequest(@"oauth2/v4/token", Method.POST);
            restRequest.AddHeader("Accept", "application/json");
            restRequest.AddHeader("Content-Type", "application/x-www-urlencoded");
restRequest.AddParameter("grant_type", "authorization_code");
restRequest.AddParameter("client_id", "id");
restRequest.AddParameter("client_secret", "secret");
restRequest.AddParameter("code", "authcode");
restRequest.AddParameter("redirect_uri", "redirectURI");

如何解决这个问题?这是为什么:\ngrant_type=authoriza\n^

字符串应作为单个参数添加

POST https://accounts.google.com/o/oauth2/token
code=4/X9lG6uWd8-MMJPElWggHZRzyFKtp.QubAT_P-GEwePvB8fYmgkJzntDnaiAI&client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code

有效载荷是一个单独的 http 请求字符串,它不是 json 格式的你得到的响应,而是 json 格式

内容类型应该是

application/x-www-form-urlencoded