0
votes

I am writing a Windows Phone 8.1 (WINRT) app.

I am using WebAuthenticationBroker to login via google+

 public async void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args)
        {
            WebAuthenticationResult result = args.WebAuthenticationResult;

            switch (result.ResponseStatus)
            {
                case WebAuthenticationStatus.Success:
                    {  
                        var response = result.ResponseData;                        
                        string responseString = result.ResponseData.ToString();
                        _authorizationCode = responseString.Substring(response.IndexOf("=") + 1);                        

                        await getAccessToken();
                        break;
                    }
                case WebAuthenticationStatus.UserCancel:
                    {

                        break;
                    }
                default:
                case WebAuthenticationStatus.ErrorHttp:
                    {               
                    break;

                    }

            }

        }


         private async Task getAccessToken()
        {

            string oauthUrl = "https://accounts.google.com/o/oauth2/token";

            HttpClient theAuthClient = new HttpClient();

              HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, oauthUrl);

                 // default case, we have an authentication code, want a refresh/access token            
                 string content = "code=" + _authorizationCode + "&" +
                     "client_id=" + singletonInstance.GoogleClientID + "&" +
                     "client_secret=" + singletonInstance.GoogleClientSecret + "&" +
                     "redirect_uri=" + singletonInstance.GoogleCallbackUrl + "&" +
                     "grant_type=authorization_code";

                 request.Method = HttpMethod.Post;

                 request.Content = new StreamContent(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)));
                 request.Content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

                 try
                 {
                     HttpResponseMessage response = await theAuthClient.SendAsync(request);
                     parseAccessToken(response);
                 }
                 catch (HttpRequestException)
                 {

                 }


        }

But:

 HttpResponseMessage response = await theAuthClient.SendAsync(request);

gives me error:

{System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))}

"Use of networking APIs requires the ID_CAP_NETWORKING capability to be defined in the application manifest."

" at MS.Internal.Modern.ClientHttpWebRequestCreator.Create(Uri uri)\r\n at System.Net.WebRequest.Create(Uri requestUri, Boolean schemeOnly)\r\n at System.Net.WebRequest.Create(Uri requestUri)\r\n at System.Net.Http.HttpClientHandler.CreateAndPrepareWebRequest(HttpRequestMessage request)\r\n at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at Merakyahoga.com.Pages.MedicalVertical.Common.MedicalGooglePlusLoginPage.d__11.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n at Merakyahoga.com.Pages.MedicalVertical.Common.MedicalGooglePlusLoginPage.d__b.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__3(Object state)\r\n at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()"

I have already enabled Internet from Capabilities on Package.appxmanifest

Actually it goes to Continuationmanager.cs:

case ActivationKind.WebAuthenticationBrokerContinuation:
                var wabPage = rootFrame.Content as IWebAuthenticationContinuable;
                if (wabPage != null)
                {
                    wabPage.ContinueWebAuthentication(args as WebAuthenticationBrokerContinuationEventArgs);
                }
                break;

Then to app.xaml.cs:

  protected override void OnActivated(IActivatedEventArgs args)
            {
                base.OnActivated(args);

                continuationManager = new ContinuationManager();

                var continuationEventArgs = args as IContinuationActivatedEventArgs;
                if (continuationEventArgs == null)
                    return;

                var frame = Window.Current.Content as Frame;
                if (frame != null)
                {
                    // Call ContinuationManager to handle continuation activation
                    continuationManager.Continue(continuationEventArgs, frame);
                }
            }

and crashes here.

2

2 Answers

0
votes

You also need to add ID_CAP_NETWORKING to Properties\WMAppManifest.xml.

0
votes

Actual problem is that i was using HTTPClient from System.Net I now replaced it to Windows.Web HTTPClient