'Google Api authorize an App running in the console
I want to manually authorize an app that runs in the console where there is no browser available.
Basically I need the same code as they are using on this website but the C# Equivalent.
I just can't seem to find the part in the C# Library that allows me to print a link to the console instead of opening a browser.
My current implementation I have just opens the browser and throws an Error if it cant.
GoogleClientSecrets? fromStream = await GoogleClientSecrets.FromStreamAsync( stream , taskCancellationToken );
UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( fromStream.Secrets
, scopes
, user
, taskCancellationToken
, new FileDataStore( nameof(Authorize) ) );
Solution 1:[1]
The GoogleWebAuthorizationBroker.AuthorizeAsync method is designed for installed applications. Installed applications will open the browser on the machine the code is running on.
The following example is designed for web applications. The application would need to be running on a web server.
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};
});
}
These are your two options unless you are able to use service account authorization. Service account authorization is intended for server to server communication between your application and an account you the developer control. It does not work with all google apis.
options
Consider running your code once locally, then copy credentials file that is created by FileDataStore onto the server along with your code. Google .net – FileDatastore demystified
print link
There used to be a method to print the authorization link but i will have to dig around in the library for it tomorrow.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DaImTo |
