私はいくつかの既存のコードがあり、突然終了しました。私は理由を理解することはできませんC#REST APIコール - コードではなく郵便番号での作業
...ここ
は私のコードです:
public static string RequestToken(string u, string pw)
{
string result = string.Empty;
string strUrl = "https://xxx.cloudforce.com/services/oauth2/token?grant_type=password&client_id=XXXX&client_secret=XXXX&username=" + u + "&password=" + pw;
HttpWebRequest tokenRequest = WebRequest.Create(strUrl) as HttpWebRequest;
Debug.Print(strUrl);
tokenRequest.Method = "POST";
try
{
using (HttpWebResponse tokenResponse = tokenRequest.GetResponse() as HttpWebResponse)
{
if (tokenResponse.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
tokenResponse.StatusCode,
tokenResponse.StatusDescription));
DataContractJsonSerializer jsonSerializer2 =
new DataContractJsonSerializer(typeof(ResponseAuthentication));
object objTokenResponse = jsonSerializer2.ReadObject(tokenResponse.GetResponseStream());
ResponseAuthentication jsonResponseAuthentication = objTokenResponse as ResponseAuthentication;
result = jsonResponseAuthentication.strAccessToken;
}
}
catch (Exception ex)
{
Debug.Print(ex.InnerException.ToString());
}
return result;
}
これはきれいに働いていた前に、私は今500 Internal Server Error
どこを取得しています。
私がPostmanを使用してデバッグしようとすると、URLを直接渡して正常に動作します。私はコードに停止を入れて、コード内で失敗したのと全く同じURLを使用しても、Postmanでは動作しますが、C#では動作しません。郵便配達のうち
、私は
{
"access_token": "XXXXXX",
"instance_url": "https://xxx.cloudforce.com",
"id": "https://login.salesforce.com/id/XXXXXX",
"token_type": "Bearer",
"issued_at": "XXXXXX",
"signature": "XXXXXX"
}
は、私が代わりにPOST
のGET
要求を試してみましたが、明確にするために...整然とした取得し、私は(ポストマンに)次の応答が返されます。
{
"error": "invalid_request",
"error_description": "must use HTTP POST"
}
ここにアイデアはありますか?
確かに、メソッドの型は 'POST'にする必要がありますか? – levent
@levent - はい、それは私が郵便配達でも使っているものです... GETは動作しません。 – gotmike
郵便配達員の成功事例でrequest.contentTypeとは何ですか? – levent