"Отказано в доступе" при вызове GoogleWebAuthorizationBroker.AuthorizeAsync
Настройка сервера: ASP.NET создается на интегрированном конвейере.net 4.5.1, работающем на iis8 на сервере 2012.
Я пытаюсь получить учетные данные в GoogleWebAuthorizationBroker от Google, но получаю сообщение "доступ запрещен".
Думая, что это, возможно, проблемы с доступом, я попытался создать хранилище IData в памяти (введено здесь)
internal class DummyData : IDataStore
{
internal class item
{
public string Key { get; set; }
public string value { get; set; }
}
internal static List<item> pKeys = new List<item>();
public async Task ClearAsync()
{
pKeys = new List<item>();
}
public async Task DeleteAsync<T>(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("Key MUST have a value");
}
var generatedKey = GenerateStoredKey(key, typeof(T));
if (pKeys.Any(x => x.Key == generatedKey))
{
pKeys.Remove(pKeys.First(x => x.Key == generatedKey));
}
}
public Task<T> GetAsync<T>(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("Key MUST have a value");
}
var generatedKey = GenerateStoredKey(key, typeof(T));
var item = pKeys.FirstOrDefault(x => x.Key == generatedKey);
T value = item == null ? default(T) : JsonConvert.DeserializeObject<T>(item.value);
return Task.FromResult<T>(value);
}
public async Task StoreAsync<T>(string key, T value)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("Key MUST have a value");
}
using (var context = new Platform4AutoDB())
{
var generatedKey = GenerateStoredKey(key, typeof(T));
string json = JsonConvert.SerializeObject(value);
var item = pKeys.FirstOrDefault(x => x.Key == generatedKey);
if (item == null)
{
pKeys.Add(new item { Key = generatedKey, value = json });
}
else
{
item.value = json;
}
}
}
private static string GenerateStoredKey(string key, Type t)
{
return string.Format("{0}-{1}", t.FullName, key);
}
}
Основной вызов, который я использую для проверки, выглядит следующим образом:
public string Test()
{
var xStore = new DummyData();
var pSecrect = p4_db.GoogleAccounts.FirstOrDefault(x => x.ClientID == m_nClientID);
if (string.IsNullOrEmpty(pSecrect.V3ClientSecret))
{
return "You are not set up to upload you tube videos.";
}
UserCredential credential;
Sec pSecret = new Sec();
pSecret.Web.client_secret = pSecrect.V3ClientSecret;
var json = new JavaScriptSerializer().Serialize(pSecret);
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows an application to upload files to the
// authenticated user's YouTube channel, but doesn't allow other types of access.
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None,
xStore
).Result;
}
return " ";
}
получение секрета клиента осуществляется через EF6 и работает без проблем.
При запуске этого на моем промежуточном сервере я получаю следующую ошибку:
System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.AggregateException: One or more errors occurred. ---> System.ComponentModel.Win32Exception: Access is denied
at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__1.MoveNext() in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis.Auth.DotNet4\OAuth2\GoogleWebAuthorizationBroker.cs:line 59
строка 59 в GoogleWebAuthorizationBroker выглядит так:
return await AuthorizeAsyncCore(initializer, scopes, user, taskCancellationToken, dataStore)
.ConfigureAwait(false)
AuthorizeAsyncCore выглядит так:
private static async Task<UserCredential> AuthorizeAsyncCore(
GoogleAuthorizationCodeFlow.Initializer initializer, IEnumerable<string> scopes, string user,
CancellationToken taskCancellationToken, IDataStore dataStore = null)
{
initializer.Scopes = scopes;
initializer.DataStore = dataStore ?? new FileDataStore(Folder);
var flow = new GoogleAuthorizationCodeFlow(initializer);
// Create an authorization code installed app instance and authorize the user.
return await new AuthorizationCodeInstalledApp(flow, new LocalServerCodeReceiver()).AuthorizeAsync
(user, taskCancellationToken).ConfigureAwait(false);
}
так как хранилище данных не является нулевым, это не должно пытаться получить доступ к диску в любом случае.
Кто-нибудь еще успешно реализовал это в аналогичной среде, или у вас есть другие идеи?
2 ответа
Я получил это работает - мой блок учетных данных теперь выглядит так:`
UserCredential credential;
var pJ = new
{
web = new
{
client_id = ConfigurationManager.AppSettings["YoutubeClientID"],
client_secret = ConfigurationManager.AppSettings["YoutubeClientSecret"],
redirect_uris = ConfigurationManager.AppSettings["YouTubeClientRedirectUris"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
}
};
var json = new JavaScriptSerializer().Serialize(pJ);
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.Youtube },
this.m_nClientID.ToString(),
CancellationToken.None,
pStore
).Result;
}
Я также на 100% уверен, что у меня был действительный токен до того, как я его нажал - по какой-то причине, по-видимому, dll возвращается к filedatastore, когда ваш токен недействителен.
Хотя это не лучшее решение, оно работает.
`
Я знаю, что это старый вопрос, но,GoogleWebAuthorizationBroker.AuthorizeAsync
используется для установленных приложений, он откроет окно веб-браузера на компьютере, на котором он запущен. Для веб-приложений вам необходимо открыть окно согласия на компьютере пользователя.
Веб-приложения должны быть настроены по-другому. Полный пример см. в документации.
public void ConfigureServices(IServiceCollection services)
{
...
// This configures Google.Apis.Auth.AspNetCore3 for use in this app.
services
.AddAuthentication(o =>
{
// This forces challenge results to be handled by Google OpenID Handler, so there's no
// need to add an AccountController that emits challenges for Login.
o.DefaultChallengeScheme = GoogleOpenIdConnectDefaults.AuthenticationScheme;
// This forces forbid results to be handled by Google OpenID Handler, which checks if
// extra scopes are required and does automatic incremental auth.
o.DefaultForbidScheme = GoogleOpenIdConnectDefaults.AuthenticationScheme;
// Default scheme that will handle everything else.
// Once a user is authenticated, the OAuth2 token info is stored in cookies.
o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogleOpenIdConnect(options =>
{
options.ClientId = {YOUR_CLIENT_ID};
options.ClientSecret = {YOUR_CLIENT_SECRET};
});
}