How to connect to google talk using agsxmpp and oauth2?

703 views Asked by At

I am developing mini chat application which uses xmpp protocol and google talk server. I found that google doesn't allow to connect to gtalk servers if application is less secure, i.e. doesn't use OAuth 2.0. I was looking for code to connect to gtalk using agsxmpp library, but I couldn't find anything. Documentation on google's oauth2 protocol has several examples showing how to use google's apis with oauth2. But, as I understood them, all the examples require to define which api are we trying to connect to. Like in following example:

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();
            ...
        }
    }
}

Here, as you see in the line

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

They specified BooksService.Scope.Books, in other words they are explicitly showing which service are they trying to connect. But there wasn't google talk service in the google's apis list. So I am confused how to securely connect to the gtalk server using agsxmpp library and google's oauth2 protocol. Can someone show me example of how to accomplish it?

1

There are 1 answers

3
Luizgrs On

This is where the Google Talks docs are in Google Developers: https://developers.google.com/talk/

I believe, in the future it'll be totally replaced by Hangouts, which does not implement XMPP.

The scope you need for Google Talks is https://www.googleapis.com/auth/googletalk.

More details https://developers.google.com/talk/jep_extensions/oauth .

Replace the books scope by it:

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

Probably you'll want to change the FileDataStore too...