HttpClient multipart / form-data POST请求

时间:2019-09-05 18:15:47

标签: c# dotnet-httpclient

在我的应用中,我试图调用Monzo API来交换授权码,但是我无法正确地编写请求。根据文档,这应该是具有以下格式的表单POST请求

http --form POST "https://api.monzo.com/oauth2/token" \
    "grant_type=authorization_code" \
    "client_id=$client_id" \
    "client_secret=$client_secret" \
    "redirect_uri=$redirect_uri" \
    "code=$authorization_code"
{
    "access_token": "access_token",
    "client_id": "client_id",
    "expires_in": 21600,
    "refresh_token": "refresh_token",
    "token_type": "Bearer",
    "user_id": "user_id"
}

我的代码如下:

var content = new
       {
           grant_type = "authorization_code",
           client_id = "oauth2client...",
           client_secret = "mnzconf.8Ckq.....",
           redirect_uri = "https://localhost:44398/callback",
           code = code
       };

       var json = JsonConvert.SerializeObject(content);

       var response = await HttpClient.PostAsync("https://api.monzo.com/oauth2/token", new StringContent(json, Encoding.UTF8, "multipart/form-data"));
  

我收到400错误的请求响应

任何人都可以看到我在做什么吗?

0 个答案:

没有答案