C#でkintoneのレコードを取得するプログラムを作っているのですが、実行時にHttpStatusが520番の「不正なリクエストです。」というエラーが出て困っています。
他の方の類似の質問を一通り見ましたが、自分の状況と一致する事象がなかったので質問させていただきます。
気になる点としては、
・C#以外にもPostmanやJavascriptで同じ値でリクエストを送信しても同じエラーとなる
・"user:pass"をBase64エンコードした値を適当な値にしてリクエストを送ると以下のエラーに変わるため、通信と認証は問題ないと思われる。
「{“code”:“CB_WA01”,“id”:“vqEFuz7heVmNL1u4YewX”,“message”:“ユーザーのパスワード認証に失敗しました。「X-Cybozu-Authorization」ヘッダーの値が正しくありません。”}」
以下が、C#のコードです。
ご回答のほどお願い致します。
try
{
stringauth=Convert.ToBase64String(Encoding.UTF8.GetBytes("userid:password"));
varreq=HttpWebRequest.Create("https://xxx.cybozu.com/k/v1/record.json?app=1&id=1");
req.Headers.Add("Host", "xxx.cybozu.com:443");
req.Headers.Add("X-Cybozu-Authorization", auth);
req.ContentType="application/json";
req.Method=HttpMethod.Get.Method;
varresponse= (HttpWebResponse)req.GetResponse();
}
catch(WebException e)
{
Console.WriteLine(e.GetType());
Console.WriteLine(e.Message);
Console.WriteLine("HttpStatus:{0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
varrespBody=newStreamReader(e.Response.GetResponseStream()).ReadToEnd();// Body取得
Console.WriteLine(respBody);
// 以下、出力結果
// > System.Net.WebException
// > The remote server returned an error: (520) 520.
// > HttpStatus:520
// > Status Description : 520
// > {"code":"CB_IL02","id":"VlZcZ3CMtK42fB9Zi7xd","message":"不正なリクエストです。"}
}