更新1整個登錄控制器現在位於片段中。GetUserDataAsync從連接器客戶端拋出「訪問被拒絕」錯誤
更新2它現在固定。問題在於,我使用模擬器運行bot,連接器客戶端不喜歡它。當bot託管並且此用戶標識與模擬器生成的用戶標識不匹配時,ConnectorClient生成用戶標識。因此訪問被拒絕。長話短說bot必須被託管,並且應該使用connectorClient的有效通道之一來訪問以接受您的請求。希望它可以幫助別人。
更新3在API路線修正了小錯誤@Vish
我一直在試圖建立一個OAuth流的應用程序。我設法從服務中獲取userInfo,但無法將訪問令牌與bot中的用戶標識綁定。
public class LoginController : ApiController
{
[HttpGet, Route("api/login")]
public RedirectResult Login(string userid)
{
return Redirect(String.Format("https://login.windows.net/microsoft.com/oauth2/authorize?response_type=code&client_id={0}&redirect_uri={1}&resource={2}&state={3}",
"MyClientId", HttpUtility.UrlEncode(Constants.apiBasePath + userid + "/authorize"),HttpUtility.UrlEncode('MyService'), userid));
}
[HttpGet, Route("api/authorize")]
public async System.Threading.Tasks.Task<HttpResponseMessage> Authorize(string state, string code)
{
string userid = state;
AuthenticationContext ac = new AuthenticationContext("https://login.windows.net/microsoft.com/oauth2/authorize/");
ClientCredential cc = new ClientCredential("MyClientId", "MyClientSecret");
AuthenticationResult ar = await ac.AcquireTokenByAuthorizationCodeAsync(code, new Uri(Constants.apiBasePath + userid + "/authorize"), cc);
if (!String.IsNullOrEmpty(ar.AccessToken))
{
var client = new ConnectorClient(Constants.botId,Constants.botSecret);
var getData = await client.Bots.GetUserDataAsync(Constants.botId, userid);
getData.Data = ar.Serialize();
var foo = await client.Bots.SetUserDataAsync(Constants.botId, userid, getData);
//return Request.CreateResponse(foo);
var response = Request.CreateResponse(HttpStatusCode.Moved);
response.Headers.Location = new Uri("/loggedin.htm", UriKind.Relative);
return response;
}
else
return Request.CreateResponse(HttpStatusCode.Unauthorized);
}
在上面的驗證結果'ar'上的代碼片段成功地生成了所有需要的數據。現在在以下幾行中,我無法從GetUserDataAsync函數獲取userData對象。它總是拋出以下錯誤。
{
"message": "An error has occurred.",
"exceptionMessage": "Access Denied",
"exceptionType": "Microsoft.Rest.HttpOperationException",
"stackTrace": " at Microsoft.Bot.Connector.ErrorHandling.HandleError[ObjectT]
}
向我們展示LoginController代碼?確保您的登錄操作沒有授權屬性或類似的東西。 – CodeNotFound
@CodeNotFound我剛剛更新了整個LoginController的問題。我確實從登錄服務獲取了令牌。 'ar'是一個完全有效的數據對象,具有我需要的全部信息。 我想我無法使用botConnector訪問botFramework數據。 –