gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:16:17 25 4
gpt4 key购买 nike

环境:.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com