LiveConect Auth (for SkyDrive) NullReferenceException (WTH)?

雨燕双飞 提交于 2019-12-25 07:47:46

问题


I followed a 43 minute video tutorial on the Channel 9 site and read the LiveConnect page where it shows code and I don't see what I'm doing wrong. It keeps giving me a NullReferenceException error and it doesn't even bring up the "Do you want to allow app X to access skydrive" thing, it just breaks immediately. I've set breakpoints everywhere but there is nothing. Just null, null everywhere.

OnNavigatedTo event:

LoadProfile();

private async void LoadProfile()
{
try
            {
                LiveAuthClient auth = new LiveAuthClient();
                LiveLoginResult loginResult = await auth.LoginAsync(new string[] { "wl.basic" });
                if (loginResult.Status == LiveConnectSessionStatus.Connected)
                {
                    this.pageTitle.Text = "Signed in.";
                }
            }
            catch (LiveAuthException exception)
            {
                this.pageTitle.Text = "Error signing in: " + exception.Message;
            }
}

And the exception says:


回答1:


I finally found a solution.

Subscribe to a button-click event or whatever, then use this code:

LoadProfile();

which calls this method:

public async void LoadProfile()
        {
            try
            {
                LiveAuthClient auth = new LiveAuthClient();
                LiveLoginResult initializeResult = await auth.InitializeAsync();
                try
                {
                    LiveLoginResult loginResult = await auth.LoginAsync(new string[] { "wl.basic" });
                    if (loginResult.Status == LiveConnectSessionStatus.Connected)
                    {
                        LiveConnectClient connect = new LiveConnectClient(auth.Session);
                        LiveOperationResult operationResult = await connect.GetAsync("me");
                        dynamic result = operationResult.Result;
                        if (result != null)
                        {
                            this.pageTitle.Text = string.Join(" ", "Hello", result.name, "!");
                        }
                        else
                        {
                            this.pageTitle.Text = "Error getting name.";
                        }
                    }
                }
                catch (LiveAuthException exception)
                {
                    this.pageTitle.Text = "Error signing in: " + exception.Message;
                }
                catch (LiveConnectException exception)
                {
                    this.pageTitle.Text = "Error calling API: " + exception.Message;
                }
            }
            catch (LiveAuthException exception)
            {
                this.pageTitle.Text = "Error initializing: " + exception.Message;
            }

        }

Before you debug, add your app to the Windows Store Dashboard. Then go back to Visual Studio, find Package.appxmanifest in Solution Explorer and add the Internet Capability. Then go to the Project menu > Store > Associate App with the Store.

Find your app's name in the list of apps that appears, select it and click Next/Finish and then debug. It should now be working.




回答2:


Please try this code instead of yours:

LiveAuthClient auth = new LiveAuthClient();
LiveLoginResult loginResult = await auth.InitializeAsync(new string[] { "wl.basic" });
if ( loginResult.Status == LiveConnectSessionStatus.Connected )
{
    LiveConnectClient connect = new LiveConnectClient( auth.Session );
    ...


来源:https://stackoverflow.com/questions/21247297/liveconect-auth-for-skydrive-nullreferenceexception-wth

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