.Net 中的 YouTube 和 OAuth 2.0

发布于 2024-12-28 09:05:06 字数 660 浏览 1 评论 0原文

有谁知道如何使用 OAuth 2.0 正确验证帐户,然后使用该身份验证令牌访问用户的 YouTube 帐户?

http://code.google.com/apis/youtube/2.0 末尾/developers_guide_protocol_oauth2.html 它说

支持 YouTube 数据 API 的 Google 数据客户端库目前不支持 OAuth 2.0。但是,一组较新的 Google API 客户端库不支持 YouTube 数据 API,但提供了 OAuth 2.0 支持。 因此,可以选择使用下面列出的这些较新的库来实现其 OAuth 2.0 功能,然后强制 Google 数据客户端库使用您已获取的 OAuth 2.0 令牌。

我的应用程序通过 OAuth 2.0 流程成功运行,并且获得了一个访问令牌,应该能够访问 youtube,但我不知道如何“强制 Google 数据客户端库使用 OAuth 2.0 令牌”。

任何示例代码都会很棒。

Liron

PS 这是一个桌面应用程序。

Does anyone know how to properly authenticate an account using OAuth 2.0 and then use that auth token to access the user's YouTube account?

At the end of http://code.google.com/apis/youtube/2.0/developers_guide_protocol_oauth2.html it says

The Google Data client libraries that support the YouTube Data API do not currently support OAuth 2.0. However, a newer set of Google API client libraries, which do not support the YouTube Data API, do provide OAuth 2.0 support.
As such, it is an option to use these newer libraries, which are listed below, for their OAuth 2.0 capabilities and then force the Google Data client library to use the OAuth 2.0 token(s) that you have obtained.

I have my application successfully running through the OAuth 2.0 process and I'm getting an access token which should be able to access youtube, but I don't know how to "force the Google Data client library to use the OAuth 2.0 token(s)".

Any example code would be great.

Liron

PS This is for a desktop application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任性一次 2025-01-04 09:05:06

为此,您需要在 google data apps (https://code.google.com/apis/console) 和 youtube api (http://code.google.com/apis/youtube) 上设置帐户/仪表板)。

然后,您必须使用其 oauth 机制对 google data api 进行身份验证。像下面这样的东西 - 这是从我们拥有的一些代码中删除的。
{code}

//Create Client     
m_Client = new NativeApplicationClient(GoogleAuthenticationServer.Description, m_ClientID, m_ClientSecret);
//Add Youtube scope to requested scopes
m_Scopes.Add("https://gdata.youtube.com");
//Get Authentication URL
authStateInitial = new AuthorizationState(m_Scopes);
authStateInitial.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = m_Client.RequestUserAuthorization(authStateInitial);

//Navigate to URL, authenticate get accessToken
string accessToken = ...;

string[] tokens = accessToken.Split(new char[] { '&' });
if(tokens.Length == 2)
{
  authStateFinal = new AuthorizationState(m_Scopes);
  authStateFinal.AccessToken = tokens[0];
  authStateFinal.RefreshToken = tokens[1];

  if(m_AuthStateInitial == null)
  {
    m_Client.RefreshToken(m_AuthStateFinal);
  }
  OAuth2Authenticator<NativeApplicationClient> authenticator = new OAuth2Authenticator<NativeApplicationClient>(m_Client, GetState); //GetState returns authStateInitial
  authenticator.LoadAccessToken();
}

然后,您必须使用从上面获得的访问令牌和 youtube 开发者密钥来验证 youtube api。
{code}

    GAuthSubRequestFactory m_Authenticator = new GAuthSubRequestFactory(ServiceNames.YouTube, "Product Name");
    m_Authenticator.Token = AccessToken;

    YouTubeService m_YouTubeService = new YouTubeService(m_Authenticator.ApplicationName, m_DeveloperKey);
    m_YouTubeService.RequestFactory = m_Authenticator;

希望这对某人有帮助。

Do do this you need to have both an account set up on google data apps (https://code.google.com/apis/console) and with the youtube apis (http://code.google.com/apis/youtube/dashboard).

You then have to authenticate the google data api using their oauth mechanisms. Something like the following - this is gutted from some code we have.
{code}

//Create Client     
m_Client = new NativeApplicationClient(GoogleAuthenticationServer.Description, m_ClientID, m_ClientSecret);
//Add Youtube scope to requested scopes
m_Scopes.Add("https://gdata.youtube.com");
//Get Authentication URL
authStateInitial = new AuthorizationState(m_Scopes);
authStateInitial.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = m_Client.RequestUserAuthorization(authStateInitial);

//Navigate to URL, authenticate get accessToken
string accessToken = ...;

string[] tokens = accessToken.Split(new char[] { '&' });
if(tokens.Length == 2)
{
  authStateFinal = new AuthorizationState(m_Scopes);
  authStateFinal.AccessToken = tokens[0];
  authStateFinal.RefreshToken = tokens[1];

  if(m_AuthStateInitial == null)
  {
    m_Client.RefreshToken(m_AuthStateFinal);
  }
  OAuth2Authenticator<NativeApplicationClient> authenticator = new OAuth2Authenticator<NativeApplicationClient>(m_Client, GetState); //GetState returns authStateInitial
  authenticator.LoadAccessToken();
}

Then you have to authenticate the youtube apis by using both the access token you got from above and the youtube Developer Key.
{code}

    GAuthSubRequestFactory m_Authenticator = new GAuthSubRequestFactory(ServiceNames.YouTube, "Product Name");
    m_Authenticator.Token = AccessToken;

    YouTubeService m_YouTubeService = new YouTubeService(m_Authenticator.ApplicationName, m_DeveloperKey);
    m_YouTubeService.RequestFactory = m_Authenticator;

Hope this helps someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文