アクセストークン取得時の400エラー

asx

2023.02.02既読 744

C#(.NET Framework 4.7.2)にて、WEBAPIを作成しています
トークルームにテキストを送信しているのですが、
現在API2.0への移行作業中です。
リフレッシュトークンは使用せず、送信の度にアクセストークンを毎回取得しています。
<<テキスト送信時>>
(1)アクセストークン取得
(2)トークルーム指定でのメッセージ送信(https://www.worksapis.com/v1.0/bots/[botId]/channels/[チャンネルID]/messages)
社内のサーバでAPIをテスト稼働させていますが、(1)のアクセストークン取得時、
400エラーが返ってくる場合と正常にアクセストークンが取得できる場合があります。
リクエスト内容は常に同じはずなのですが、何か原因は考えられますでしょうか
社内のネットワークが遅い場合があるのですが、原因となりますでしょうか?
ざっくりとですが、アクセストークン取得の処理は下記のとおりです。
using (var rsa = System.Security.Cryptography.RSA.Create())
{
   rsa.FromXmlString(System.IO.File.ReadAllText(new System.IO.FileInfo(privateKeyXml).FullName));
   Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor descriptor = new Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor
   {
       Issuer = clientId,
       Claims = new System.Collections.Generic.Dictionary<string, object>() { ["sub"] = serviceAccount },
       SigningCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(new Microsoft.IdentityModel.Tokens.RsaSecurityKey(rsa), "RS256"),
       IssuedAt = System.DateTime.UtcNow,
       Expires = System.DateTime.UtcNow.AddMinutes(60),
   };
   var aaa = handler.WriteToken(handler.CreateJwtSecurityToken(descriptor));
   string postData = "assertion=" + aaa + "&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer" + "&client_id=" + client_id + "&client_secret=" + client_secret + "&scope=" + scope;
   //バイト型配列に変換
   byte[] postDataBytes = System.Text.Encoding.ASCII.GetBytes(postData);
   HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://auth.worksmobile.com/oauth2/v2.0/token");
   HttpWebResponse webres = null;
   req.Method = "POST";
   req.ContentType = "application/x-www-form-urlencoded";
   req.ContentLength = postDataBytes.Length;
   System.IO.Stream reqStream = req.GetRequestStream();
   reqStream.Write(postDataBytes, 0, postDataBytes.Length);
   reqStream.Close();
   webres = (HttpWebResponse)req.GetResponse();
   System.IO.Stream resStream = webres.GetResponseStream();
   System.IO.StreamReader sr = new System.IO.StreamReader(resStream);
   string s = sr.ReadToEnd();
   sr.Close();
   var dic = System.Text.Json.JsonSerializer.Deserialize<System.Collections.Generic.Dictionary<string, string>>(s);
   accessToken = dic["access_token"];
   if (accessToken == null || accessToken == "")
   {
       errMes = "AccessTokenが取得できませんでした";
       return false;
   }
   return true;
}

コメント0

前の投稿 MS Power Automateに、カレンダー予定追加トリガーを作成したい
次の投稿API 1.0 > 2.0の移行作業
リスト

まだ、解決できませんか?
今すぐ実際に使用しているLINE WORKSユーザーに質問してみましょう。