0

我正在开发使用 xmpp 协议和谷歌谈话服务器的迷你聊天应用程序。我发现如果应用程序不太安全,谷歌不允许连接到 gtalk 服务器,即不使用 OAuth 2.0。我正在寻找使用 agsxmpp 库连接到 gtalk 的代码,但我找不到任何东西。关于 google 的 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,换句话说,他们明确地显示了他们试图连接的服务。但是 google 的 apis 列表中没有 google talk 服务。所以我很困惑如何使用 agsxmpp 库和谷歌的 oauth2 协议安全地连接到 gtalk 服务器。有人可以告诉我如何完成它的例子吗?

4

1 回答 1

1

这是 Google Talks 文档在 Google Developers 中的位置: https ://developers.google.com/talk/

我相信,将来它会完全被Hangouts所取代,它没有实现 XMPP

Google Talks 所需的范围是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 ...

于 2014-12-01T15:48:55.467 回答