"使用网络API需要在应用程序清单中定义ID_CAP_NETWORKING功能。"

时间:2015-02-26 09:22:40

标签: c# windows multithreading windows-phone windows-phone-8.1

我正在撰写 Windows Phone 8.1(WINRT)应用

我正在使用 WebAuthenticationBroker 通过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)
                 {

                 }


        }

可是:

 HttpResponseMessage response = await theAuthClient.SendAsync(request);

给我错误:

  

{System.UnauthorizedAccessException:访问被拒绝。 (例外   HRESULT:0x80070005(E_ACCESSDENIED))}

     

“使用网络API需要ID_CAP_NETWORKING功能   在应用程序清单中定义。“

     

“在MS.Internal.Modern.ClientHttpWebRequestCreator.Create(Uri)   uri)\ r \ n在System.Net.WebRequest.Create(Uri requestUri,Boolean   schemeOnly)\ r \ n在System.Net.WebRequest.Create(Uri requestUri)\ r \ n   在   System.Net.Http.HttpClientHandler.CreateAndPrepareWebRequest(HttpRequestMessage   请求)\ r \ n at   System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage   request,CancellationToken cancellationToken)\ r \ n ---堆栈结束   从抛出异常的先前位置追踪--- \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\ r \ n at   Merakyahoga.com.Pages.MedicalVertical.Common.MedicalGooglePlusLoginPage.d__11.MoveNext(个)\ r \ n ---   从抛出异常的先前位置开始的堆栈跟踪结束   --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.GetResult()\ r \ n at   Merakyahoga.com.Pages.MedicalVertical.Common.MedicalGooglePlusLoginPage.d__b.MoveNext(个)\ r \ n ---   从抛出异常的先前位置开始的堆栈跟踪结束   --- \ r \ n在System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__3(对象   国家)\ r \ n at   System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()“

我已经从 Package.appxmanifest

功能启用了互联网

实际上它转到Continuationmanager.cs:

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

然后到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);
                }
            }

并在这里崩溃。

2 个答案:

答案 0 :(得分:0)

您还需要将ID_CAP_NETWORKING添加到Properties\WMAppManifest.xml

答案 1 :(得分:0)

实际问题是我在使用System.Net的HTTPClient 我现在将其替换为Windows.Web HTTPClient