Tôi đang cố gắng để POST yêu cầu sử dụng RestSharp khách hàng như sau tôi đang đi qua Bộ luật Auth để sau chức năngLàm thế nào để POST yêu cầu sử dụng RestSharp
public void ExchangeCodeForToken(string code)
{
if (string.IsNullOrEmpty(code))
{
OnAuthenticationFailed();
}
else
{
var request = new RestRequest(this.TokenEndPoint, Method.POST);
request.AddParameter("code", code);
request.AddParameter("client_id", this.ClientId);
request.AddParameter("client_secret", this.Secret);
request.AddParameter("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
request.AddParameter("grant_type", "authorization_code");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
client.ExecuteAsync<AuthResult>(request, GetAccessToken);
}
}
void GetAccessToken(IRestResponse<AuthResult> response)
{
if (response == null || response.StatusCode != HttpStatusCode.OK
|| response.Data == null
|| string.IsNullOrEmpty(response.Data.access_token))
{
OnAuthenticationFailed();
}
else
{
Debug.Assert(response.Data != null);
AuthResult = response.Data;
OnAuthenticated();
}
}
Nhưng tôi đang nhận được response.StatusCode = Bad Request. Bất cứ ai có thể giúp tôi làm thế nào để tôi POST yêu cầu bằng cách sử dụng Restsharp khách hàng.
chuỗi jsonToSend = JsonHelper.ToJson (json); Bạn có thể giải thích dòng này? –
nó chỉ chuyển đổi đối tượng thành chuỗi json. (json = object, jsonToSend = biểu diễn json của "json"). Tôi nên thay đổi những tên đó. – David
Cách đính kèm tệp vào yêu cầu của bạn? – OPV