【问题标题】:Google OAuth2 sample not working?Google OAuth2 示例不起作用?
【发布时间】:2015-09-17 16:40:33
【问题描述】:

我正在尝试学习如何使用 google API,并且我已经开始使用可以在此处找到的示例: https://developers.google.com/drive/web/quickstart/dotnet

我相信我已经完全按照说明进行操作,并且我已经复制粘贴了所有代码,但我无法让它工作。

如果我调试它,当我到达 GoogleWebAuthorizationBroker.AuthorizeAsync 方法时,Visual Studio 会询问我 GoogleClientSecrets.cs 的位置。

如果我继续,代码会抛出 HttpListenerException 消息(翻译自瑞典语)“提供的网络名称格式错误”。错误代码是1214。

我猜它此时应该打开浏览器,但它没有,这可能是问题吗?

这对我来说是全新的,所以我很难找到解决方案。一段时间以来,我一直在寻找解决方案。

任何建议都会非常感谢。

这是完整的代码:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DriveQuickstart
{
    class Program
    {
        static string[] Scopes = { DriveService.Scope.DriveReadonly };
        static string ApplicationName = "Drive API Quickstart";

        static void Main(string[] args)
        {
            UserCredential credential;

            using (var stream =
                new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        {
                string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            Console.WriteLine("Credential file saved to: " + credPath);
        }

        // Create Drive API service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

        // Define parameters of request.
        FilesResource.ListRequest listRequest = service.Files.List();
        listRequest.MaxResults = 10;

        // List files.
        IList<Google.Apis.Drive.v2.Data.File> files = listRequest.Execute()
            .Items;
        Console.WriteLine("Files:");
        if (files != null && files.Count > 0)
        {
            foreach (var file in files)
            {
                Console.WriteLine("{0} ({1})", file.Title, file.Id);
            }
        }
        else
        {
            Console.WriteLine("No files found.");
        }
        Console.Read();

    }
  }

}

【问题讨论】:

  • 有人吗?我在这里发疯了。通过谷歌搜索和测试一段时间,我的意思是每天晚上几天...我在这里发疯...

标签: c# google-oauth


【解决方案1】:

好吧,我不知道为什么这不起作用,但我找到了另一种方法:跳过 Drive API 并使用 GData api。

可以在此处找到对我来说完美无缺的示例: https://developers.google.com/google-apps/spreadsheets/?hl=en

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多