Google将API驱动到C# [英] Google drive API to C#

查看:165
本文介绍了Google将API驱动到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



但是我遇到了同样的问题:未处理的类型异常 Google.GData.Client.GDataRequestException'发生在Google.GData.Client.dll中。

其他信息:执行身份验证请求返回意外结果:404



我相信我正在输入正确的登录名和密码。



这里是代码:

  ///< summary> 
/// MainWindow.xaml的交互逻辑
///< / summary>
public partial class MainWindow:Window
{
public MainWindow()
{
InitializeComponent();
SpreadsheetsService myService = new SpreadsheetsService(exampleCo-exampleApp-1);
myService.setUserCredentials(myusername,password);
SpreadsheetQuery query = new SpreadsheetQuery();
SpreadsheetFeed feed = myService.Query(query);

Console.WriteLine(Your spreadsheets:);
foreach(feed.Entries中的SpreadsheetEntry条目)
{
Console.WriteLine(entry.Title.Text);
}

}
}


解决方案

GData API中的旧认证方法已被弃用。您需要使用OAuth2。






该示例要求您使用以下nuget包及其依赖项:




  • Google.GData.Spreadsheets



另外,您必须请转至 https://console.developers.google.com 并注册您的应用程序并为其创建凭据你可以输入你的CLIENT_ID和CLIENT_SECRET。



这是我用来组合这个例子的文档: https://developers.google.com/google-apps/spreadsheets/

 使用System; 
使用System.Windows.Forms;
使用Google.GData.Client;
使用Google.GData.Spreadsheets;

命名空间ConsoleApplication4
{
类程序
{
static void Main(string [] args)
{
string CLIENT_ID =YOUR_CLIENT_ID;
string CLIENT_SECRET =YOUR_CLIENT_SECRET;
字符串SCOPE =https://spreadsheets.google.com/feeds https://docs.google.com/feeds;
string REDIRECT_URI =urn:ietf:wg:oauth:2.0:oob;

OAuth2Parameters parameters = new OAuth2Parameters();

parameters.ClientId = CLIENT_ID;
parameters.ClientSecret = CLIENT_SECRET;
parameters.RedirectUri = REDIRECT_URI;
parameters.Scope = SCOPE;

string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
MessageBox.Show(authorizationUrl);
Console.WriteLine(请访问消息框中的URL以授权您的OAuth
+请求令牌。一旦完成,请将您的访问代码键入
+继续...);
parameters.AccessCode = Console.ReadLine();

OAuthUtil.GetAccessToken(parameters);
string accessToken = parameters.AccessToken;
Console.WriteLine(OAuth访问令牌:+ accessToken);

GOAuth2RequestFactory requestFactory = $ b $ new GOAuth2RequestFactory(null,MySpreadsheetIntegration-v1,parameters);
SpreadsheetsService service = new SpreadsheetsService(MySpreadsheetIntegration-v1);
service.RequestFactory = requestFactory;

SpreadsheetQuery query = new SpreadsheetQuery();

SpreadsheetFeed feed = service.Query(query);

//遍历所有电子表格返回
foreach(Feed.Entries中的SpreadsheetEntry条目)
{
//将此电子表格的标题打印到屏幕上
Console.WriteLine(entry.Title.Text);
}
Console.ReadLine();
}
}
}


Hi I am trying to read a spreadsheet using google drive API using the google api example.

however I getting the same failure: "An unhandled exception of type 'Google.GData.Client.GDataRequestException' occurred in Google.GData.Client.dll

Additional information: Execution of authentication request returned unexpected result: 404"

I am sure I am entering the right login and password.

here is the code:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        SpreadsheetsService myService = new SpreadsheetsService("exampleCo-exampleApp-1");
        myService.setUserCredentials(myusername, password);            
        SpreadsheetQuery query = new SpreadsheetQuery();
        SpreadsheetFeed feed = myService.Query(query);

        Console.WriteLine("Your spreadsheets:");
        foreach (SpreadsheetEntry entry in feed.Entries)
        {
            Console.WriteLine(entry.Title.Text);
        }

    }
}

解决方案

The old authentication methods in the GData API have been deprecated. You need to use OAuth2.


This example requires you to use the following nuget packages and their dependencies:

  • Google.GData.Spreadsheets

Also, you must go to https://console.developers.google.com and register your application and create credentials for it so you can enter your CLIENT_ID and CLIENT_SECRET.

This is the documentation I used to put together this example: https://developers.google.com/google-apps/spreadsheets/

using System;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Spreadsheets;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string CLIENT_ID = "YOUR_CLIENT_ID";
            string CLIENT_SECRET = "YOUR_CLIENT_SECRET";
            string SCOPE = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds";
            string REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

            OAuth2Parameters parameters = new OAuth2Parameters();

            parameters.ClientId = CLIENT_ID;
            parameters.ClientSecret = CLIENT_SECRET;
            parameters.RedirectUri = REDIRECT_URI;
            parameters.Scope = SCOPE;

            string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
            MessageBox.Show(authorizationUrl);
            Console.WriteLine("Please visit the URL in the message box to authorize your OAuth "
              + "request token.  Once that is complete, type in your access code to "
              + "continue...");
            parameters.AccessCode = Console.ReadLine();

            OAuthUtil.GetAccessToken(parameters);
            string accessToken = parameters.AccessToken;
            Console.WriteLine("OAuth Access Token: " + accessToken);

            GOAuth2RequestFactory requestFactory =
                new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
            SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
            service.RequestFactory = requestFactory;

            SpreadsheetQuery query = new SpreadsheetQuery();

            SpreadsheetFeed feed = service.Query(query);

            // Iterate through all of the spreadsheets returned
            foreach (SpreadsheetEntry entry in feed.Entries)
            {
                // Print the title of this spreadsheet to the screen
                Console.WriteLine(entry.Title.Text);
            }
            Console.ReadLine();
        }
    }
}

这篇关于Google将API驱动到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆