2

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

我正在尝试从谷歌的 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 应用程序中执行此操作的最佳方法是什么。

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

提前致谢..!!!

4

1 回答 1

0

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

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;
于 2018-02-20T10:01:20.963 回答