1

Hier ist mein Code. Es ist schon über einen Monat Ich versuche Kalender in Outlook hinzufügen, aber nichts funktioniert :(bitte helfen. Die Funktion AcquireTokenByAuthorizationCodeAsync nie abgeschlossen ist. Und die token.Result ist immer nullAcquireTokenByAuthorizationCodeAsync wird nicht abgeschlossen

string authority = ConfigurationManager.AppSettings["authority"]; 
string clientID = ConfigurationManager.AppSettings["clientID"]; 
Uri clientAppUri = new Uri(ConfigurationManager.AppSettings["clientAppUri"]); 
string serverName = ConfigurationManager.AppSettings["serverName"]; 

var code = Request.Params["code"]; 
AuthenticationContext ac = new AuthenticationContext(authority, true); 

ClientCredential clcred = new ClientCredential(clientid, secretkey); 

//ac = ac.AcquireToken(serverName, clientID, clientAppUri, PromptBehavior.Auto); 
//string to = ac.AcquireToken(serverName, clientID, clientAppUri, PromptBehavior.Auto).AccessToken; 
var token = ac.AcquireTokenByAuthorizationCodeAsync(code, new Uri("http://localhost:2694/GetAuthCode/Index/"), clcred, resource: "https://graph.microsoft.com/"); 
string newtoken = token.Result.AccessToken; 
ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2013); 
exchangeService.Url = new Uri("https://outlook.office365.com/" + "ews/exchange.asmx"); 
exchangeService.TraceEnabled = true; 
exchangeService.TraceFlags = TraceFlags.All; 
exchangeService.Credentials = new OAuthCredentials(token.Result.AccessToken); 
exchangeService.FindFolders(WellKnownFolderName.Root, new FolderView(10)); 

Appointment app = new Appointment(exchangeService); 
app.Subject = ""; 
app.Body = ""; 
app.Location = ""; 
app.Start = DateTime.Now; 
app.End = DateTime.Now.AddDays(1); 
app.Save(SendInvitationsMode.SendToAllAndSaveCopy); 

Antwort

0

ich dieses Problem erlebt haben, wenn ich rufe die AcquireTokenByAuthorizationCodeAsyncresult statt mit await Schlüsselwort und all diesen Code wurde in einem asynchron im MVC-Controller

Wenn Sie in dem gleichen Szenario sind, können Sie dieses Problem auf zwei Arten beheben können.

1. Immer mit dem async, await wie Code unten:

public async System.Threading.Tasks.Task<ActionResult> About() 
{ 
    ... 
    var result =await ac.AcquireTokenByAuthorizationCodeAsync(code, new Uri("http://localhost:2694/GetAuthCode/Index/"), clcred, resource: "https://graph.microsoft.com/"); 
    var accessToken = result.AccessToken; 
    ... 
} 

2.Use der Synchronisations-Controller:

public void About() 
{ 
    ... 
    var result =ac.AcquireTokenByAuthorizationCodeAsync(code, new Uri("http://localhost:2694/GetAuthCode/Index/"), clcred, resource: "https://graph.microsoft.com/").Result; 
    var accessToken = result.AccessToken; 
    ... 
} 
+0

Noch funktioniert nicht –

Verwandte Themen