2012-01-23 94 views
2

有誰知道如何使用OAuth 2.0正確認證帳戶,然後使用該認證令牌訪問用戶的YouTube帳戶?.Net中的YouTube和OAuth 2.0

截至http://code.google.com/apis/youtube/2.0/developers_guide_protocol_oauth2.html末它說

支持YouTube數據API目前不支持,OAuth 2.0的谷歌數據客戶端庫。但是,一組不支持YouTube數據API的較新版Google API客戶端庫確實提供了OAuth 2.0支持。 因此,您可以選擇使用下面列出的這些較新的庫來獲取OAuth 2.0功能,然後強制Google數據客戶端庫使用您獲得的OAuth 2.0令牌。

我有我的應用程序成功完成OAuth 2.0運行過程中,我得到一個訪問令牌,該令牌應該能夠訪問YouTube,但我不知道如何「迫使谷歌數據客戶端庫使用OAuth 2.0令牌「。

任何示例代碼都會很好。

Liron

PS這是一個桌面應用程序。

回答

3

爲此,您需要同時在Google數據應用(https://code.google.com/apis/console)上設置帳戶,並使用youtube apis(http://code.google.com的/ apis /的YouTube /儀表板)。

然後你必須使用他們的oauth機制來驗證谷歌數據api。類似於以下內容 - 這是從我們的一些代碼中解脫出來的。 {}代碼

//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; 

希望這可以幫助別人。