c# - ASP.net Web 应用程序(非 MVC): Accessing Google GoogleWebAuthorizationBroker. AuthorizeAsync 返回拒绝访问错误

标签 c# google-oauth google-api-dotnet-client access-denied proximityapi

环境:.net 4.5.1 集成管道上的 ASP.NET 简单 Web 应用程序在服务器 2012 上的 iis8 上运行,不遵循 MVC。

我正在尝试从谷歌的 GoogleWebAuthorizationBroker 获取凭据,但我一直收到“访问被拒绝”...即使我在“授权的 JavaScript 来源”和“授权的重定向 URI”中允许了我的网址

以下已安装应用程序的 URL 实现 https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#installed-applications

这是我的代码片段

var folder = "F:\\MyApp\\WebApp\\MyGoogleStorage";

string[] scopes = new string[] {
            Google.Apis.Proximitybeacon.v1beta1.ProximitybeaconService.Scope.UserlocationBeaconRegistry
        };

        ClientSecrets secrets = new ClientSecrets()
        {
            ClientId = CLIENT_ID,
            ClientSecret = CLIENT_SECRET
        };

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        secrets,
                        scopes,
                        "user",
                        CancellationToken.None,
                        new FileDataStore(folder)).Result;

并使用另一种方式创建凭据

            using (var stream = new FileStream(Utility.AppPath + "/client_secrets.json", FileMode.Open, FileAccess.Read))
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                scopes,
                "user", CancellationToken.None, new FileDataStore(folder));
        }

但在这两种情况下我都被拒绝访问。

我的假设是,它正在发生,因为我正在尝试使用样本 “已安装的应用程序”

请告诉我在 .net 简单 Web 应用程序中最好的方法是什么。

如果有人成功完成,也分享一些代码。

提前致谢..!!!

最佳答案

对于这个解决方案,我发现是

string serviceAccountEmail = "proximitybeacon@proximitytest-1234.iam.gserviceaccount.com"; // Service Account Email
    string userEmail = "abc@gmail.com"; //Email of yours 

    //.p12 file is having all the details about the Service Account we create a Cryptography certificate by it. This you need to download fron your project which you make in Google Developer Console. Foolow stesps from this link https://webapps.stackexchange.com/questions/58411/how-where-to-obtain-a-p12-key-file-from-the-google-developers-console

    X509Certificate2 certificate = new X509Certificate2("F://yorrappPath/ProximityTest-2d889bd4fa49.p12", "notasecret", X509KeyStorageFlags.Exportable); // F://yorrappPath --  Give proper location of .p12 file

    ServiceAccountCredential credential = new ServiceAccountCredential(
       new ServiceAccountCredential.Initializer(serviceAccountEmail)
       {
           User = userEmail,
           Scopes = new string[] { "https://www.googleapis.com/auth/userlocation.beacon.registry" }
       }.FromCertificate(certificate));

    if (!credential.RequestAccessTokenAsync(CancellationToken.None).Result)
    {
        return "Can't get the access token";
    }

    //Beacon Object with its values
    Google.Apis.Proximitybeacon.v1beta1.Data.Beacon beacon = new Google.Apis.Proximitybeacon.v1beta1.Data.Beacon();
    Google.Apis.Proximitybeacon.v1beta1.Data.AdvertisedId advertisedId = new Google.Apis.Proximitybeacon.v1beta1.Data.AdvertisedId();
    beacon.AdvertisedId = new Google.Apis.Proximitybeacon.v1beta1.Data.AdvertisedId() { Id = "ZgCC7BLy8FXla3VmnnibWQ==", Type = "EDDYSTONE" };
    beacon.BeaconName = "99911";
    beacon.Status = "ACTIVE";
    beacon.LatLng = new Google.Apis.Proximitybeacon.v1beta1.Data.LatLng() { Latitude = 28.38, Longitude = 77.12 };
    beacon.ExpectedStability = "STABLE";

    //Beacon Service for register which having HttpClientInitializer(credential with token detail)
    Google.Apis.Proximitybeacon.v1beta1.ProximitybeaconService service = new Google.Apis.Proximitybeacon.v1beta1.ProximitybeaconService(new BaseClientService.Initializer()
    {
        ApplicationName = "proximityTest", // Replace that with you App name which you given in Google
        HttpClientInitializer = credential
    });
    var registerRequest = new Google.Apis.Proximitybeacon.v1beta1.BeaconsResource.RegisterRequest(service, beacon);

    //uncomment this for update beacons. 3 parameter is important for update BeaconName
    //var updateRequest = new Google.Apis.Proximitybeacon.v1beta1.BeaconsResource.UpdateRequest(service, beacon, "beacons/3!660082ec12f2f055e56b75669e789b59");
    //updateRequest.Execute();

    //uncomment this for getting list of beacons.
    // var listRequest = new Google.Apis.Proximitybeacon.v1beta1.BeaconsResource.ListRequest(service);
    // return listRequest.Execute();

    try
    {
        //This will register a Beacon
        registerRequest.Execute();
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
    return beacon;

关于c# - ASP.net Web 应用程序(非 MVC): Accessing Google GoogleWebAuthorizationBroker. AuthorizeAsync 返回拒绝访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38981615/

相关文章:

google-cloud-platform - Google Cloud Platform - 无法添加范围(script.external_request 范围)

c# - 如何使用 Google.Apis.YouTube.v3 和 C# 将视频上传到 youtube?

google-drive-api - 与谷歌服务帐户共享了一个谷歌驱动器文件夹,但服务帐户看不到它

c# - 比较两个目录时遇到问题

c# - 更改为 x64 平台时无法加载程序集错误

c# - 将表达式从 C# 转换为 Java

ios - 如何修复 WKWebView 上受损的 Google 登录/注册?

google-calendar-api - Google 日历 v3 推送通知到期

c# - 如果浏览器关闭,GoogleWebAuthorizationBroker.AuthorizeAsync() 会挂起

c# - WCF服务中具有C#的json?