【问题标题】:youtube api use oauth token to get info on user in .NETyoutube api 使用 oauth 令牌在 .NET 中获取用户信息
【发布时间】:2016-03-20 10:46:44
【问题描述】:

我有一个应用程序(ios),你可以用谷歌登录,我要求用户授予访问他的 youtube 数据的权限,

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");

【问题讨论】:

    标签: c# ios .net youtube youtube-api


    【解决方案1】:

    好的,我找到了我自己问题的答案, 将访问令牌发送到服务器,然后使用网络客户端调用具有正确标题的 youtube 数据 api v3,结果将是 youtube 播放列表 ids,从那里获取 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());
            }
    
            }
    

    【讨论】:

      猜你喜欢
      • 2018-07-15
      • 2015-04-28
      • 2020-02-04
      • 2015-10-18
      • 2015-04-18
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 2016-11-10
      相关资源
      最近更新 更多