How to utilize code flow for OneDrive API?

主宰稳场 提交于 2019-12-23 02:28:41

问题


How to allow my C# apps (non-forms, non-web based) application authenticate & interact with OneDrive? (That is to say, completely programmatically.)

So far:

-Application registered, have client id & secret.
-Enabled both web & phone.
-Personal, not Business.
-Client token flow appears to be for interactive, so using code.
-Tried the OneDrive SDK, but it just kept saying it couldn't authenticate (not why). -Now hand-rolling requests.

As web client code:

    async Task<string> GetStringFromURLAsync(string theURL)
    {
        string urlContents = "";
        System.Net.Http.HttpClient c = null;
        try
        {
            c = new System.Net.Http.HttpClient();
            c.Timeout = new System.TimeSpan(0, 0, 30); // 30 seconds
            Task<string> asyncResp = c.GetStringAsync(theURL);
            urlContents = await asyncResp;
            Console.WriteLine("success...");
        }
        [catch...]
        return urlContents;
    }

Called by:

    Task<string> tGetContent = GetStringFromURLAsync(ai.fullTokenRequestURI);
    string httpResponseBody = tGetContent.Result;

(ai is a struct holding client id, url, etc.)

If it is not possible to roll your own and get the correct responses, then which SDK should I be using?

This fares no better:

    this.oneDriveClient = OneDriveClient.GetMicrosoftAccountClient(
                       authInfo.cient_id,
                       authInfo.redirect_url,
                       authInfo.scopes);
    await this.oneDriveClient.AuthenticateAsync();

The OD API simply complains:

    OneDrive reported the following error:
    Code: AuthenticationFailure
    Message: Failed to retrieve a valid authentication token for the user.

来源:https://stackoverflow.com/questions/38229475/how-to-utilize-code-flow-for-onedrive-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!