【问题标题】:"Use of networking APIs requires the ID_CAP_NETWORKING capability to be defined in the application manifest."“使用网络 API 需要在应用程序清单中定义 ID_CAP_NETWORKING 功能。”
【发布时间】:2015-02-26 09:22:39
【问题描述】:

我正在编写一个 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 在 System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage 请求,CancellationToken cancelToken)\r\n--- 堆栈结束 从先前引发异常的位置跟踪 ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 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 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 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(Object 状态)\r\n 在 System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()"

我已经从 Package.appxmanifest

上的 Capabilities 启用 Internet

实际上是转到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);
                }
            }

并在此处崩溃。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2012-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-26
        • 1970-01-01
        • 2011-08-13
        • 2016-04-25
        • 1970-01-01
        相关资源
        最近更新 更多