2015-12-14 82 views
0

我有一個應用程序(IOS),您可以登錄谷歌和我要求用戶授予權限才能訪問他的YouTube數據,的YouTube API使用OAuth令牌來獲取信息的用戶在.NET

func doOAuthGoogle(){ 
    let oauthswift = OAuth2Swift(
     consumerKey: GoogleYoutube["consumerKey"]!, 
     consumerSecret: GoogleYoutube["consumerSecret"]!, 
     authorizeUrl: "https://accounts.google.com/o/oauth2/auth", 
     accessTokenUrl: "https://accounts.google.com/o/oauth2/token", 
     responseType: "code" 
    ) 
    oauthswift.authorizeWithCallbackURL(NSURL(string: "W2GCW24QXG.com.xxx.xxx:/oauth-swift")!, scope: "https://www.googleapis.com/auth/youtube", state: "", success: { 
     credential, response, parameters in 
     print("oauth_token:\(credential.oauth_token)") 
     let parameters = Dictionary<String, AnyObject>() 

     //TODO: send oauth to server 
     Alamofire.request(.GET, "http://xxx.azurewebsites.net:80/api/Login/Google/", parameters: ["access_token" : credential.oauth_token]).responseJSON { response in 
      print(response) 
      let resultDic : NSDictionary = (response.result.value as? NSDictionary)! 

      defaults.setObject(resultDic.valueForKey("userId"), forKey: "UserId") 
      let vc = self.storyboard?.instantiateViewControllerWithIdentifier("vcGroupsViewController") as? GroupsController 
      self.navigationController?.pushViewController(vc!, animated: true) 
        } 

     }, failure: {(error:NSError!) -> Void in 
      print("ERROR: \(error.localizedDescription)") 
    }) 
} 

之後,我得到credential.oauth_token並將其發送到.NET服務器。

我有庫

using Google.Apis.Services; 
using Google.Apis.YouTube.v3; 

,現在我想獲得用戶的音樂播放列表,但是我不能在有facebookclient Facebook上找到該示例代碼,就像在服務器上

var accessToken = access_token; 
    var client = new FacebookClient(accessToken); 
    // 1 : hit graph\me?fields= 
    dynamic me = client.Get("me", new { fields = new[] { "id", "name", "first_name", "last_name", "picture.type(large)", "email", "updated_time" } }); 
    dynamic meMusic = client.Get("me/music"); 

回答

0

好吧,我找到了我自己的問題的答案, 發送訪問令牌到服務器,然後使用Web客戶端調用帶有適當標頭的YouTube數據api v3,結果將是YouTube播放列表ID,從那裏取watchHistory節點並保存到本地var,並在webclient2中使用它來調用列表項。

希望它能幫助別人。

internal User SaveGoogleYoutubeLogin(string access_token) 
{ 
    var accessToken = access_token; 
    string watchHistoryList = ""; 
    using (WebClient webclient = new WebClient()) 
    { 
     webclient.Headers.Add(HttpRequestHeader.Authorization,"Bearer " + accessToken); 
     webclient.Headers.Add("X-JavaScript-User-Agent", "Google APIs Explorer"); 
     var respone2 = webclient.DownloadString("https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true&key={your_api_key}"); 
     Debug.Print(respone2); 
     JObject jResponse = JObject.Parse(respone2); 
     watchHistoryList = (string)jResponse["items"][0]["contentDetails"]["relatedPlaylists"]["watchHistory"].ToString(); 

    } 
    using (WebClient webclient2 = new WebClient()) 
    { 
     webclient2.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + accessToken); 
     webclient2.Headers.Add("X-JavaScript-User-Agent", "Google APIs Explorer"); 
     var respone2 = webclient2.DownloadString("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId="+ watchHistoryList + "&key={your_api_key}"); 
     Debug.Print(respone2); 
     JObject jResponse = JObject.Parse(respone2); 
     foreach (var item in jResponse["items"]) 
     { 
      Debug.Print(item.ToString()); 
     } 

     } 
相關問題