问题
I am using Youtube Data API V3 to upload videos to my youtube account using Oauth 2.0 authentication.
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
Here during the authentication process it shows up login and Consent screen to get the authorization as shown
Oauth_UserConsent
I need to overcome the above, so that without user consent I want the youtube data API V3 to be authenticated using OAuth 2.0 and do video upload to my account.
So I found that ‘Service Accounts’ can be used for this kind of scenarios but again found that service account do not work with Youtube data API because Service Accounts require an associated YouTube channel, and you cannot associate new or existing channels with service accounts for the link https://developers.google.com/youtube/v3/guides/moving_to_oauth Is there any alternative way in dotnet to skip the user consent and get authenticated programmatically and do a programmatically upload of videos to my account
回答1:
It's only the first time a user authenticates that he will have to accept the terms (allowing your app to access his resources...). From that point, the library stores that access and refresh tokens. The access token is valid for 60 minutes, and when it expires the library uses the refresh token to refresh the access token and get a valid one.
If you don't want to have user consent at all, you should use the ServiceAccount flow as described in https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#service-account
回答2:
Update for V3
See this SO answer here for working .NET web example. For me, the big change from the Command Prompt example provided by Google is the change in obtaining credentials from the GoogleWebAuthorizationBroker.AuthorizeAsync()
to GoogleAuthorizationCodeFlow
(See Linked answer for details)
You need to obtain a refresh token and store that in your code along with the OAuth Key and Secret.
UPDATE: I have come up with my own solution to do exactly this. I use .NET to make an HTTP/REST call to obtain the access_token and then pass it to JavaScript to perform the actual upload giving you the advantage of progress indication.
See my Answer/Solution here https://stackoverflow.com/a/49516167/3790921
来源:https://stackoverflow.com/questions/34633200/auto-upload-of-videos-to-my-account-using-youtube-data-api-v3-without-user-cons