Youtube API Authentication C#

本文关键字:Authentication API Youtube | 更新日期: 2023-09-27 18:29:29

我正在使用此方法验证并将评论发布到YouTube,但它不起作用,我想知道这是在C#中验证YouTube API 2.0的正确方法吗?

        YouTubeRequestSettings reqSettings = new YouTubeRequestSettings("Youtube Comments", devkey, "gmail address", "password");

我可能需要指定客户端ID,但不确定在哪里?

Youtube API Authentication C#

您可以使用AuthSub代理身份验证或ClientLogin用户名/密码身份验证来验证请求。

若要使用YouTube API执行任何操作,请创建YouTubeRequestSettings对象,该对象指定要使用的身份验证信息和身份验证方案。使用该对象,您可以创建一个YouTubeRequest对象,用于实际执行操作。(如果您在创建YouTubeRequestSettings对象时没有指定身份验证信息,那么您将只能使用YouTubeRequest对象执行不需要身份验证的操作。)

YouTubeRequestSettings settings = 
  new YouTubeRequestSettings("example app", clientID, developerKey);
YouTubeRequest request = new YouTubeRequest(settings);

上面的例子取自这里,您是否可以看到AuthSub代理身份验证或ClientLogin身份验证的所有细节

https://developers.google.com/youtube/2.0/developers_guide_dotnet#Authentication

要获得开发人员密钥和客户端ID,请访问http://code.google.com/apis/youtube/dashboard

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="Google.GData.Client" %>
<%@ Import Namespace="Google.GData.Extensions" %>
<%@ Import Namespace="Google.GData.Calendar" %>
<%@ Import Namespace="System.Net" %>
<script runat="server">
    void PrintCalendar() {
        GAuthSubRequestFactory authFactory = new GAuthSubRequestFactory("cl", "TesterApp");
        authFactory.Token = (String) Session["token"];
        CalendarService service = new CalendarService(authFactory.ApplicationName);
        service.RequestFactory = authFactory;
        EventQuery query = new EventQuery();
        query.Uri = new Uri("http://www.google.com/calendar/feeds/default/private/full");
        try
        {
            EventFeed calFeed = service.Query(query);
            foreach (Google.GData.Calendar.EventEntry entry in calFeed.Entries)
            {
                Response.Write("Event: " + entry.Title.Text + "<br/>");
            }
        }
        catch (GDataRequestException gdre)
        {
            HttpWebResponse response = (HttpWebResponse)gdre.Response;
            //bad auth token, clear session and refresh the page
            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                Session.Clear();
                Response.Redirect(Request.Url.AbsolutePath, true);
            }
            else
            {
                Response.Write("Error processing request: " + gdre.ToString());
            }
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test Site</title>
</head>
<body>
    <form id="form1" runat="server">
    <h1>AuthSub Sample Page</h1>
    <div>
    <%
        GotoAuthSubLink.Visible = false;
        if (Session["token"] != null)
        {
            PrintCalendar();
        }
        else if (Request.QueryString["token"] != null)
        {
            String token = Request.QueryString["token"];
            Session["token"] = AuthSubUtil.exchangeForSessionToken(token, null).ToString();
            Response.Redirect(Request.Url.AbsolutePath, true);
        }
        else //no auth data, print link
        {
            GotoAuthSubLink.Text = "Login to your Google Account";
            GotoAuthSubLink.Visible = true;
            GotoAuthSubLink.NavigateUrl = AuthSubUtil.getRequestUrl(Request.Url.ToString(),
                "http://www.google.com/calendar/feeds/",false,true);
        }
     %>
    <asp:HyperLink ID="GotoAuthSubLink" runat="server"/>
    </div>
    </form>
</body>
</html>

以上示例取自

https://developers.google.com/gdata/articles/authsub_dotnet