- Microsoft.Identity.Client
< li>System.Net.Http.Json.
Код: Выделить всё
public class GoogleAuthService
{
private readonly IPublicClientApplication _pca;
public GoogleAuthService()
{
_pca = PublicClientApplicationBuilder.Create("OAuth 2.0 client ID")
.WithRedirectUri("com.googleusercontent.apps.OAuth 2.0 client ID:/oauth2redirect")
.Build();
}
public async Task AuthenticateAsync()
{
try
{
// Scopes for accessing Google APIs
string[] scopes = { "https://www.googleapis.com/auth/androidpublisher" };
var result = await _pca.AcquireTokenInteractive(scopes)
.WithParentActivityOrWindow(MainActivity.CurrentActivity)
.ExecuteAsync();
Console.WriteLine($"GoogleAPIAccessToken: {result.AccessToken}");
return result.AccessToken; // Use this access token for API calls
}
catch (Exception ex)
{
// Handle authentication errors
Console.WriteLine($"Authentication failed: {ex.Message}");
return null;
}
}
}
Код: Выделить всё
public static MainActivity CurrentActivity { get; private set; }
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
CurrentActivity = this;
Instance = this;
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);
}
Код: Выделить всё
Код: Выделить всё
private readonly GoogleAuthService _googleAuthService;
_googleAuthService = new GoogleAuthService();
public async void LoginButtonClicked(Object sender, EventArgs e)
{
string token = await _googleAuthService.AuthenticateAsync();
}
< blockquote>
Мы не можем выполнить ваш запрос
unauthorized_client: клиент не существует или не доступен для потребителей. Если вы являетесь разработчиком приложения, настройте новое приложение с помощью регистрации приложений на портале Azure по адресу https://go.microsoft.com/fwlink/?linkid=2083908.
Снимок экрана:
[img]https:/ /i.sstatic.net/65A3cT1B.jpg[/img]
Итак, я не могу сгенерировать токен аутентификации с помощью OAuth 2.0. Правильный ли этот процесс? Как сгенерировать токен аутентификации для вызова API разработчика Google?
Предложите решение этой проблемы.
Подробнее здесь: https://stackoverflow.com/questions/790 ... -developer
Мобильная версия