YouTube and OAuth 2.0 in .Net

五迷三道 提交于 2019-12-09 13:48:20

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/8971775/youtube-and-oauth-2-0-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!