youtube api使用oauth令牌获取.NET用户的信息

时间:2015-12-14 20:13:40

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

我有一个应用程序(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;

现在我想获取用户音乐播放列表,但我无法找到该示例代码,例如在facebook中有facebookclient

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

1 个答案:

答案 0 :(得分:0)

好的,我找到了自己的答案, 将访问令牌发送到服务器,然后使用Web客户端使用正确的标头调用youtube数据api v3,结果将是youtube播放列表ID,从那里获取#pragma omp single节点并将其保存到本地var,并使用它在webclient2中调用列表项。

希望它能帮助别人。

watchHistory