-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Google API .NET Client - How do I get OAuth2 Access Token and Refresh token for C# ASP.NET Core Web API client to authenticate YouTube Data API v3 #1739
Comments
There's no way to authenticate users like this in Google, from a user name and password like you seem to be doing for Azure. You will never have access to the user's Google account password to do something like this. You always need to first obtain the access and refresh tokens for the user, whit the user present, i.e. through some UI. When your application (the one with the UI, can be web, mobile or desktop) asks Google for the tokens, Google will redirect the user to Google's own authentication page, where the user will enter their Google email and password and potentially grant access to any resource your app has requested access through scopes. Once Google has authenticated the user, it will redirect to your specified redirect URI sending the access and refresh tokens. |
Can you please provide a solution for this type of scenario. The Web API needs to authenticate and access my YouTube business channel and upload the video programmatically. So there is use case for my scenario, can you provide a solution? Software Architecture have geared towards Micro-services, so there should be service or API to API authentication without users being present |
If you want your server to use your YouTube account, then you can authenticate that yourself to get an access token and a refresh token which you'd probably store in a database. You wouldn't go through the initial authentication process within your web app, because the users of the web app don't have the relevant information to authenticate. |
As @jskeet said, for your server to use your own You Tube account, you can write a quick console app to authenticate yourself and get your access and refresh tokens, store them in a secure location that your backend can access and use them from there. Your Console app should have this code (or very similar):
|
Many thanks for your help. This looks like it will work. |
You should see the beam of smile on my cheeks... |
Solved from code snippets above |
hi @amanda-tarafa i'm hoping you can help me with my scenario. i can understand how my web UI can implement oauth to get the access and refresh tokens. once that's done i can send that back to my API, that part is clear. what's unclear is: how can i use the access token in the .NET google client libraries? i see the GoogleWebAuthorizationBroker, but that wants to open a window in the browser. ideally, i'd like to be able to just set my access token and then proceed with the upload. any thoughts? |
If you send the access token to your API and it's in your API where you want to access Google services then you can do as follows in your API code (example with YouTube, but will work the same with any of the others): YouTubeService service = new YouTubeService(new BaseClientService.Initializer
{
HttpClientInitializer = Google.Apis.Auth.OAuth2.GoogleCredential.FromAccessToken(accesToken),
...
}); One thing to note is that you would have to send a fresh access token from the client appliction to your API everytime, access tokens usually have a 1 hour lifetime. For that, on the client application where you are performing the authentication and calling your API, you have to store the |
thank you @amanda-tarafa ! i see the last version that has that class is 1.44.1. can you speak to why that was removed? |
How do I get OAuth2 Access Token and Refresh token for C# ASP.NET Core Web API client to authenticate YouTube Data API v3
There is no UI for a username to manually enter their username and password, then receive code to get the token in this scenario. No redirect_uri is required.
How can I get the access token and refresh token
I once solved a similar issue with Microsoft Azure AD, solution on stackoverflow
I just can't find any information regarding Google Cloud Platform .NET clients for this scenario
The text was updated successfully, but these errors were encountered: