Environment: ASP.NET simple web application on .net 4.5.1 integrated pipeline running on iis8 on server 2012 which is not following MVC.

I'm attempting to get credentials from google's GoogleWebAuthorizationBroker but i keep getting "access is denied"...Even i allowed my urls in "Authorized JavaScript origins" and "Authorized redirect URIs"

Following below URL implementation for Installed App https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#installed-applications

Here are mine code snippet

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;

And use another way to create credentials

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

But in both cases i am getting access denied.

My assumption is that, it's happening because i am trying to use sample "Installed Applications"

Please advice me what is the best way to do that in .net simple web application.

Also share some code if any one done it successfully.

Thanks in advance..!!!

1

There are 1 answers

0
Deepak Kumar On

For this solution work around I found was

string serviceAccountEmail = "[email protected]"; // Service Account Email
    string userEmail = "[email protected]"; //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;