.Net中的YouTube和OAuth 2.0 [英] YouTube and OAuth 2.0 in .Net

查看:119
本文介绍了.Net中的YouTube和OAuth 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



http://code.google.com/apis/youtube/2.0/developers_guide_protocol_oauth2.html


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


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



任何示例代码



Liron



PS这是用于桌面应用程序。

解决方案

要执行此操作,您需要同时在Google数据应用(https://code.google.com/apis/console)上设置一个帐户并



然后,您必须使用其api身份验证机制来对google数据api进行身份验证。



。类似于以下内容-这是从我们拥有的一些代码中删除的。
{code}

  //创建客户端
m_Client = new NativeApplicationClient(GoogleAuthenticationServer.Description,m_ClientID, m_ClientSecret);
//将YouTube作用域添加到请求的作用域
m_Scopes.Add( https://gdata.youtube.com);
//获取身份验证URL
authStateInitial = new AuthorizationState(m_Scopes);
authStateInitial.Callback =新的Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = m_Client.RequestUserAuthorization(authStateInitial);

//导航到URL,验证get accessToken
字符串accessToken = ...;

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

if(m_AuthStateInitial == null)
{
m_Client.RefreshToken(m_AuthStateFinal);
}
OAuth2Authenticator< NativeApplicationClient> authenticator =新的OAuth2Authenticator< NativeApplicationClient>(m_Client,GetState); // GetState返回authStateInitial
authenticator.LoadAccessToken();
}

然后,您必须使用获得的访问令牌对youtube api进行身份验证从上方以及youtube Developer Key。
{code}

  GAuthSubRequestFactory m_Authenticator = new GAuthSubRequestFactory(ServiceNames.YouTube,产品名称); 
m_Authenticator.Token = AccessToken;

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

希望这对某人有帮助。


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.

解决方案

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.

这篇关于.Net中的YouTube和OAuth 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆