C# “使用网络API需要在应用程序清单中定义ID_CAP_网络功能。”
我正在编写一个Windows Phone 8.1 WINRT应用程序 我正在使用WebAuthenticationBroker通过google登录+C# “使用网络API需要在应用程序清单中定义ID_CAP_网络功能。”,c#,windows,multithreading,windows-phone,windows-phone-8.1,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.WebAuthenticationRe
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)
{
}
}
但是:
给我一个错误:
{System.UnauthorizedAccessException:访问被拒绝。来自的异常
HRESULT:0x80070005 E_ACCESSDENIED}
使用网络API需要ID_CAP_网络功能
在应用程序清单中定义
在MS.Internal.Modern.ClientHttpWebRequestCreator.CreateUri
uri\r\n位于System.Net.WebRequest.CreateUri requestUri,布尔值
schemeOnly\r\n位于System.Net.WebRequest.CreateUri requestUri\r\n
在
System.Net.Http.HttpClientHandler.CreateAndPrepareWebRequestHttpRequestMessage
请求\r\n在
System.Net.Http.HttpClientHandler.SendAsyncHttpRequestMessage
请求,CancellationToken CancellationToken\r\n--堆栈结束
从引发异常的上一个位置的跟踪--\r\n
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccessTask
任务\r\n位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotificationTask
任务\r\n位于
System.Runtime.CompilerServices.TaskWaiter`1.GetResult\r\n位于
Merakyahoga.com.Pages.MedicalVertical.Common.MedicalGooglePlusLoginPage.d\u 11.MoveNext\r\n--
来自引发异常的上一个位置的堆栈结束跟踪
--\r\n位于System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccessTask
任务\r\n位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotificationTask
任务\r\n位于
System.Runtime.CompilerServices.TaskWaiter.GetResult\r\n位于
Merakyahoga.com.Pages.MedicalVertical.Common.MedicalGooglePlusLoginPage.d\u b.MoveNext\r\n--
来自引发异常的上一个位置的堆栈结束跟踪
--\r\n位于System.Runtime.CompilerServices.AsyncMethodBuilderCore.b\u 3Object
状态\r\n位于
System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore
我已经从Package.appxmanifest上的功能启用了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);
}
}
并且在此崩溃。您还需要将ID\u CAP\u网络添加到Properties\WMAppManifest.xml。实际问题是我使用的是System.Net中的HTTPClient 我现在将其替换为Windows.Web HTTPClient
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);
}
}