Azure WebSites拒绝访问Google Api

时间:2015-04-24 14:11:44

标签: asp.net google-api google-calendar-api azure-web-sites access-denied

我正在处理Web表单应用程序使用的fmemopen。 dll的一项功能是创建Google dll并使用它。当我在本地主机上运行webforms时,一切都运行顺畅。但是,如果我从Azure网站运行它

  

' GoogleWebAuthorizationBroker.AuthorizeAsync'方法抛出   HttpListenerException(0x5):访问被拒绝

这是一个堆栈跟踪:

CalendarService

这就是我设置凭据的方式

[HttpListenerException (0x5): Access is denied]
Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +82   
Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task) +71   
Mosoft.Integrations.CalendarService.<GetCredentials>d__2.MoveNext() +362

[AggregateException: One or more errors occurred.]
System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) +3833113
System.Threading.Tasks.Task`1.GetResultCore(Boolean aitCompletionNotification) +73
System.Threading.Tasks.Task`1.get_Result() +10900681
Mosoft.Integrations.CalendarService.GoogleCalendar..ctor(Int32 userID) +38
WebFormsDemo.ModalCreate.CreateServices() +35
WebFormsDemo.ModalCreate.Page_Load(Object sender, EventArgs e) +240 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

有什么建议可能有什么不对吗?或指向正确的方向?

2 个答案:

答案 0 :(得分:1)

两者都在使用服务帐户,所以我希望它能为您提供帮助。

可能性#1:

生成P12证书时,您是否尝试更改

var certificate = new X509Certificate2(p12Path, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);

//(notice the X509KeyStorageFlags.MachineKeySet |)

Possability#2,使用服务帐户进行身份验证

string keyFilePath = System.Web.Hosting.HostingEnvironment.MapPath("~/----path to json credentials downloaded using google developer console ---.json");
string p12Path = System.Web.Hosting.HostingEnvironment.MapPath("~/---path to p12 certificate ----.p12");
string[] scopes = new string[] {
    CalendarService.Scope.Calendar, // Manage your calendars
    CalendarService.Scope.CalendarReadonly // View your Calendars
};

       var certificate = new X509Certificate2(p12Path, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer("--email generated by google developer console ending with ----@developer.gserviceaccount.com")
    {
        Scopes = scopes
    }.FromCertificate(certificate));

    CalendarService service = new CalendarService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = "----the project name used from developer console---",
    });

答案 1 :(得分:0)

 UserCredential googleCredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(secrets,
            new[] { Google.Apis.Calendar.v3.CalendarService.Scope.Calendar }, userID.ToString(), CancellationToken.None, new MyDataStore());

这似乎不是使用网络应用进行身份验证的正确方法。 This answer是我停止“访问被拒绝”错误的方法。