在不使用setUserCredentials的情况下添加Google日历条目

时间:2011-05-09 08:56:32

标签: asp.net dotnetopenauth google-apps

我正在关注Google为市场应用提供的示例

http://code.google.com/googleapps/marketplace/tutorial_dotnet.html

我按照示例中的方式运行了Google身份验证, 我的下一个任务是在Google日历中添加一个条目。我找到了以下代码,它也工作正常

CalendarService service = new CalendarService(APPLICATION_NAME);


            service.setUserCredentials(vUserName, vPassword);

            Google.GData.Calendar.EventEntry entry = new Google.GData.Calendar.EventEntry();

            // Set the title and content of the entry.
            entry.Title.Text = title;
            entry.Content.Content = contents;

            // Set a location for the event.
            Where eventLocation = new Where();
            eventLocation.ValueString = location;
            entry.Locations.Add(eventLocation);

            When eventTime = new When(startTime, endTime);
            entry.Times.Add(eventTime);

            Uri postUri = new Uri("http://www.google.com/calendar/feeds/default/private/full");

            // Send the request and receive the response:
            AtomEntry insertedEntry = service.Insert(postUri, entry);

我遇到的问题是以下行,如果我提供我的用户名和密码,它将起作用

service.setUserCredentials(vUserName, vPassword);

我已在google示例中对用户进行了身份验证。所以我不知道其他用户使用他们的Gmail登录我网站的用户名和密码。

如何使用我拥有的信息添加日历条目?

我见过几个RequestFactory验证用户的例子。但找不到我可以使用的完整示例

1 个答案:

答案 0 :(得分:1)

  1. 您需要创建一个.pfx证书文件并将其上传到谷歌并将其放在您的服务器上。
  2. 创建您的AuthSubRequest网址

    <asp:HyperLink ID="GotoAuthSubLink" runat="server"/>
    GotoAuthSubLink.Text = "Login to your Google Account"; 
    GotoAuthSubLink.NavigateUrl = AuthSubUtil.getRequestUrl("(return url)http://www.example.com/RetrieveToken", "https://www.google.com/calendar/feeds/", false, true);
    
  3. 在用户点击您的身份验证链接后,他们会返回您的返回网址。获取会话令牌如下

    String sessionToken = ""; //Save this for making your calls.
    String certFile = "D:\\websites\\yourwebsite.com\\google.pfx";
    String result = GetAuthSubSessionToken(Request["token"]);
    
    protected AsymmetricAlgorithm GetRsaKey()
    {
        X509Certificate2 cert = new X509Certificate2(certFile, ""); 
        RSACryptoServiceProvider privateKey = cert.PrivateKey as RSACryptoServiceProvider;
        return privateKey;
    }
    
    public string GetAuthSubSessionToken(string singleUseToken)
    {
        string gatStr = "";
        try
        {
            AsymmetricAlgorithm rsaKey = GetRsaKey();
            try
            {
                sessionToken = AuthSubUtil.exchangeForSessionToken(singleUseToken, rsaKey).ToString();
                gatStr = "Session Token = " + SessionToken;
            }
            catch (Exception e)
            {
                gatStr = "Error: I appears that the Google authentication server is experiencing an error. Try the authorizaton link again in a few minutes. <a href=\""
                         + rtnUrl + "\" title=\"" + e.Message + "\">continue</a>";
            }
        }
        catch (Exception E)
        {
            gatStr = "Error: rsa " + E.Message + E.StackTrace;
        }
        return gatStr;
    }
    
  4. 保存会话令牌,并在后续调用中使用CreateCalendarService来创建日历服务。

    public CalendarService CreateCalendarService()
    {
        GAuthSubRequestFactory authFactory = new GAuthSubRequestFactory("cl", "YourName-calendarApp-1");
        authFactory.Token = sessionToken;
        authFactory.PrivateKey = GetRsaKey();
        CalendarService cs = new CalendarService(authFactory.ApplicationName);
        cs.RequestFactory = authFactory;
        return cs;
    }