【问题标题】:How to connect to google talk using agsxmpp and oauth2?如何使用 agsxmpp 和 oauth2 连接到 google talk?
【发布时间】:2014-11-29 12:05:57
【问题描述】:

我正在开发使用 xmpp 协议和谷歌谈话服务器的迷你聊天应用程序。我发现如果应用程序是less secure, i.e. doesn't use OAuth 2.0,谷歌不允许连接到 gtalk 服务器。我正在寻找使用 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 服务器。 有人可以告诉我如何完成它的例子吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    您需要的 Google Talks 范围是https://www.googleapis.com/auth/googletalk

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

    用它替换 books 范围:

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

    您可能也想更改 FileDataStore...

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    相关资源
    最近更新 更多