如何使用agsxmpp和oauth2连接到google talk?

时间:2014-11-29 12:05:57

标签: c# .net oauth-2.0 google-talk agsxmpp

我正在开发使用xmpp协议和google talk服务器的迷你聊天应用程序。我发现如果应用程序为less secure, i.e. doesn't use OAuth 2.0,谷歌不允许连接到gtalk服务器。我正在寻找使用agsxmpp库连接到gtalk的代码,但我找不到任何东西。关于谷歌oauth2协议的文档有几个例子说明了如何将google的apis与oauth2一起使用。但是,正如我所理解的那样,所有示例都需要定义我们尝试连接的api。 如下例所示:

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Books.v1;
using Google.Apis.Books.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;

namespace Books.ListMyLibrary
{
    /// <summary>
    /// Sample which demonstrates how to use the Books API.
    /// https://code.google.com/apis/books/docs/v1/getting_started.html
    /// <summary>
    internal class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine("Books API Sample: List MyLibrary");
            Console.WriteLine("================================");
            try
            {
                new Program().Run().Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    Console.WriteLine("ERROR: " + e.Message);
                }
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        private async Task Run()
        {
            UserCredential credential;
            using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { BooksService.Scope.Books },
                    "user", CancellationToken.None, new FileDataStore("Books.ListMyLibrary"));
            }

            // Create the service.
            var service = new BooksService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Books API Sample",
                });

            var bookshelves = await service.Mylibrary.Bookshelves.List().ExecuteAsync();
            ...
        }
    }
}

在这里,正如您在行中看到的那样

credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        new[] { BooksService.Scope.Books },
                        "user", CancellationToken.None, new FileDataStore("Books.ListMyLibrary")); 

他们指定了BooksService.Scope.Books,换句话说,他们明确地显示了他们尝试连接的服务。但谷歌的apis列表中没有google talk服务。所以我很困惑如何使用agsxmpp库和google的oauth2协议安全地连接到gtalk服务器。 有人能告诉我如何实现它的例子吗?

1 个答案:

答案 0 :(得分:1)

这是谷歌开发人员使用谷歌会谈文档的地方: https://developers.google.com/talk/

我相信,将来它会被Hangoutswhich does not implement XMPP完全取代。

Google会谈所需的范围是https://www.googleapis.com/auth/googletalk

更多详情https://developers.google.com/talk/jep_extensions/oauth

用它替换书籍范围:

credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        new[] { "https://www.googleapis.com/auth/googletalk" },
                        "user", CancellationToken.None, new FileDataStore("Books.ListMyLibrary")); 

你可能也想要改变FileDataStore ......