c# - Google Calendar API for UWP Windows 10 Application One or more errors occurred -


i'm trying use google calendar api v3, have problems while running codes, gives me error :

an exception of type 'system.aggregateexception' occurred in mscorlib.ni.dll not handled in user code additional information: 1 or more errors occurred.

i don't know why does, should work well. here screenshot : exception

also codes :

 usercredential credential;                 credential = googlewebauthorizationbroker.authorizeasync(                    new uri("ms-appx:///assets/client_secrets.json"),                     scopes,                     "user",                     cancellationtoken.none).result;               // create google calendar api service.             var service = new calendarservice(new baseclientservice.initializer()             {                 httpclientinitializer = credential,                 applicationname = applicationname,             });          var calendarservice = new calendarservice(new baseclientservice.initializer         {             httpclientinitializer = credential,             applicationname = "windows 10 calendar sample"         });         var calendarlistresource = await calendarservice.calendarlist.list().executeasync(); 

if can @ least calling through rest api, great too, must consider it's uwp, has way work well. tried through rest api, "request error code 400".

thanks attention.

the google api client library .net not support uwp now. can't use google.apis.calendar.v3 client library in uwp apps now. more info, please see similar question: universal windows platform app google calendar.

to use google calendar api in uwp, can call through rest api. use rest api, need authorize requests first. how authorize requests, please see authorizing requests google calendar api , using oauth 2.0 mobile , desktop applications.

after have access token, can call calendar api following:

var clientid = "{your client id}"; var redirecturi = "pw.oauth2:/oauth2redirect"; var scope = "https://www.googleapis.com/auth/calendar.readonly"; var spotifyurl = $"https://accounts.google.com/o/oauth2/auth?client_id={clientid}&redirect_uri={uri.escapedatastring(redirecturi)}&response_type=code&scope={uri.escapedatastring(scope)}"; var starturi = new uri(spotifyurl); var enduri = new uri(redirecturi);  // authorization code webauthenticationresult webauthenticationresult = await webauthenticationbroker.authenticateasync(webauthenticationoptions.none, starturi, enduri); if (webauthenticationresult.responsestatus == webauthenticationstatus.success) {     var decoder = new wwwformurldecoder(new uri(webauthenticationresult.responsedata).query);     if (decoder[0].name != "code")     {         system.diagnostics.debug.writeline($"oauth authorization error: {decoder.getfirstvaluebyname("error")}.");         return;     }      var autorizationcode = decoder.getfirstvaluebyname("code");       //get access token     var pairs = new dictionary<string, string>();     pairs.add("code", autorizationcode);     pairs.add("client_id", clientid);     pairs.add("redirect_uri", redirecturi);     pairs.add("grant_type", "authorization_code");      var formcontent = new windows.web.http.httpformurlencodedcontent(pairs);      var client = new windows.web.http.httpclient();     var httpresponsemessage = await client.postasync(new uri("https://www.googleapis.com/oauth2/v4/token"), formcontent);     if (!httpresponsemessage.issuccessstatuscode)     {         system.diagnostics.debug.writeline($"oauth authorization error: {httpresponsemessage.statuscode}.");         return;     }      string jsonstring = await httpresponsemessage.content.readasstringasync();     var jsonobject = windows.data.json.jsonobject.parse(jsonstring);     var accesstoken = jsonobject["access_token"].getstring();       //call google calendar api     using (var httprequest = new windows.web.http.httprequestmessage())     {         string calendarapi = "https://www.googleapis.com/calendar/v3/users/me/calendarlist";          httprequest.method = windows.web.http.httpmethod.get;         httprequest.requesturi = new uri(calendarapi);         httprequest.headers.authorization = new windows.web.http.headers.httpcredentialsheadervalue("bearer", accesstoken);          var response = await client.sendrequestasync(httprequest);          if (response.issuccessstatuscode)         {             var liststring = await response.content.readasstringasync();             //todo         }     } } 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -