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

时间:2016-08-16 18:03:39

标签: 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

以下是我的代码段

            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));
        }

并使用其他方式创建凭据

POST

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

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

请告诉我在.net简单Web应用程序中执行此操作的最佳方法。

如果任何一个代码成功完成,也会共享一些代码。

提前致谢.. !!!

1 个答案:

答案 0 :(得分: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;