0

我正在使用这种方法对YouTube 进行身份验证并向 YouTube 发布评论,但它不起作用,我想知道这是在 C# 中验证 YouTube API 2.0 的正确方法吗?

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

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

4

1 回答 1

1

您可以使用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

于 2013-08-28T21:52:22.097 回答