c# - Saving a file in OneDrive in a .NET application -
i'm doing .net
application in c#
in addition other features, user can log in onedrive
account , save file account. downloaded code windows desktop (.net) guided setup
teaches register application , authentication user , part running proposed. however, need know how put part save file in account
, root folder of onedrive
(the user log account , enter file want save in account , saved in root folder). i've seen examples in github
, web applications.
update
using microsoft.identity.client; public partial class app : application { private static string clientid = "your_client_id_here"; public static publicclientapplication publicclientapp = new publicclientapplication(clientid); } public partial class mainwindow : window { string _graphapiendpoint = "https://graph.microsoft.com/v1.0/me"; string[] _scopes = new string[] { "user.read" }; public mainwindow() { initializecomponent(); } private async void callgraphbutton_click(object sender, routedeventargs e) { authenticationresult authresult = null; try { if (authresult == null) { authresult = await app.publicclientapp.acquiretokensilentasync(_scopes, app.publicclientapp.users.firstordefault()); } } catch (msaluirequiredexception ex) { // msaluirequiredexception happened on acquiretokensilentasync. indicates need call acquiretokenasync acquire token system.diagnostics.debug.writeline($"msaluirequiredexception: {ex.message}"); try { authresult = await app.publicclientapp.acquiretokenasync(_scopes); } catch (msalexception msalex) { resulttext.text = $"error acquiring token:{system.environment.newline}{msalex}"; } } catch (exception ex) { resulttext.text = $"error acquiring token silently:{system.environment.newline}{ex}"; return; } if (authresult != null) { resulttext.text = await gethttpcontentwithtoken(_graphapiendpoint, authresult.accesstoken); displaybasictokeninfo(authresult); this.signoutbutton.visibility = visibility.visible; } } } public async task<string> gethttpcontentwithtoken(string url, string token) { var httpclient = new system.net.http.httpclient(); system.net.http.httpresponsemessage response; try { var request = new system.net.http.httprequestmessage(system.net.http.httpmethod.get, url); //add token in authorization header request.headers.authorization = new system.net.http.headers.authenticationheadervalue("bearer", token); response = await httpclient.sendasync(request); var content = await response.content.readasstringasync(); return content; } catch (exception ex) { return ex.tostring(); } }
reference: windows desktop (.net) guided setup
Comments
Post a Comment