LiveConect Auth (for SkyDrive) NullReferenceExcept

2019-09-13 21:35发布

问题:

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 );
    ...