添加 Google 日历条目而不使用 setUserCredentials

发布于 2024-11-06 01:52:03 字数 1457 浏览 5 评论 0 原文

我正在遵循 Google for Marketplace 应用程序在 http://code.google 上提供的示例

。 com/googleapps/marketplace/tutorial_dotnet.html

我得到了谷歌身份验证,如示例所示, 我的下一个任务是向 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);

我已经像谷歌示例中那样对用户进行了身份验证。所以我不知道其他用户使用 gmail 登录我的网站的用户名和密码。

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

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

I am following the example provided by Google for Market place app at

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

I got the google authentication working as in the example ,
My next task is to add a entry to Google calendar. I found following code for that, and it is also working fine

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

The problem i have is the following line, If i give my username and password it will work

service.setUserCredentials(vUserName, vPassword);

i have authenticated the user as in google example. So I don’t know the username and password of other users login to my site using their gmail.

How do i add a calender entry with the information i have?

I have seen several examples with RequestFactory authenticating the user. but couldn't find complete example that I can use

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝色星空 2024-11-13 01:52:03
  1. 您需要创建一个 .pfx 证书文件并将其上传到 google 并将其放置在您的服务器上。
  2. 创建您的 AuthSubRequest URL

    ;
    GotoAuthSubLink.Text = "登录您的 Google 帐户"; 
    GotoAuthSubLink.NavigateUrl = AuthSubUtil.getRequestUrl("(返回网址)http://www.example.com/RetrieveToken", "https://www.google.com/calendar/feeds/", false, true);
    
  3. 当用户点击您的身份验证链接后,他们将返回到您的返回 URL。按如下方式获取您的会话令牌

    String sessionToken = ""; //保存它以供调用。
    String certFile = "D:\\websites\\yourwebsite.com\\google.pfx";
    字符串结果 = GetAuthSubSessionToken(Request["token"]);
    
    受保护的非对称算法 GetRsaKey()
    {
        X509Certificate2 cert = new X509Certificate2(certFile, ""); 
        RSACryptoServiceProvider privateKey = cert.PrivateKey 作为 RSACryptoServiceProvider;
        返回私钥;
    }
    
    公共字符串 GetAuthSubSessionToken(字符串 singleUseToken)
    {
        字符串 gatStr = "";
        尝试
        {
            非对称算法 rsaKey = GetRsaKey();
            尝试
            {
                sessionToken = AuthSubUtil.exchangeForSessionToken(singleUseToken, rsaKey).ToString();
                gatStr = "会话令牌 = " + SessionToken;
            }
            捕获(异常 e)
            {
                gatStr = "错误:我发现 Google 身份验证服务器遇到错误。请在几分钟后重试授权链接。继续";
            }
        }
        捕获(异常 E)
        {
            gatStr =“错误:rsa”+E.Message+E.StackTrace;
        }
        返回 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;
        返回CS;
    }
    
  1. you will need to create a .pfx cert file and upload it to google and place it on your server.
  2. create your AuthSubRequest URL

    <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. after the person clicks on your auth link they are returned to your return url. get your session token as follows

    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. save the session token and use CreateCalendarService in subsequent calls to create your calendar service.

    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;
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文