Google OATH2电子表格错误:无法找到指定的文件

时间:2015-09-05 21:34:10

标签: c# google-api

在尝试授权C#应用程序访问电子表格时,我遇到了这个错误:

System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=4.0.5.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
File name: 'Newtonsoft.Json, Version=4.0.5.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
   at Google.GData.Client.OAuthBase.GetOAuth2AccessToken(OAuth2Parameters parameters, String requestBody)
   at Google.GData.Client.OAuthUtil.GetAccessToken(OAuth2Parameters parameters)
   at LibraryBookLister.Authorize.GetAuthorized() in C:\Users\John\Documents\Visual Studio 2013\Projects\LibraryBookLister\LibraryBookLister\Authorize.cs:line 54

=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=4.0.5.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
 (Fully-specified)
LOG: Appbase = file:///C:/Users/John/Documents/Visual Studio 2013/Projects/LibraryBookLister/LibraryBookLister/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : Google.GData.Client, Version=2.2.0.0, Culture=neutral, PublicKeyToken=04a59ca9b0273830.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\John\Documents\Visual Studio 2013\Projects\LibraryBookLister\LibraryBookLister\bin\Debug\LibraryBookLister.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=4.0.5.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: The same bind was seen before, and was failed with hr = 0x80070002.

我以前从未见过这个,我想知道我做错了什么。 尝试获取访问令牌时会发生此错误。

以下是我班上的完整代码:

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace LibraryBookLister
{
    public partial class Authorize : Form
    {
        public Authorize()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }




        OAuth2Parameters parameters = new OAuth2Parameters();
        public void GetAuthorized()
        {


            string clientId = "[ClientID]";      // From https://console.developers.google.com
            string clientSecret = "[ClientSecret]";          // From https://console.developers.google.com
            string SCOPE = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds";
            string REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
            parameters.ClientId = clientId;
            parameters.ClientSecret = clientSecret;
            parameters.RedirectUri = REDIRECT_URI;
            parameters.Scope = SCOPE;
            SpreadsheetsService service = new SpreadsheetsService("GPLHS Activity Monitor");
            if (textCode.Text == "")
            {


                string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
                Process.Start(authorizationUrl);
                return;
            }

            else
            {
                parameters.AccessCode = textCode.Text;
                try
                {
                    OAuthUtil.GetAccessToken(parameters);

                }
                catch(Exception e) { MessageBox.Show("Bad Token.   " + e.ToString()); System.Windows.Forms.Clipboard.SetText(e.ToString()); return; }
                string accessToken = parameters.AccessToken;
                StreamWriter sw = new StreamWriter(@"C:\Users\John\AppData\Roaming\Tokens\Token.txt", false);

                string refresht = parameters.RefreshToken.ToString();
                sw.Write(refresht);
                sw.Close();

                MessageBox.Show( "OAuth Access Token: " + accessToken);
                parameters.TokenExpiry.AddYears(2016);
                parameters.RefreshToken = parameters.RefreshToken;
                //        MessageBox.Show(parameters.TokenExpiry.ToLongDateString());

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GetAuthorized();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

发现了这个问题。打开包管理器并安装: Newtonsoft.Json 这将解决问题!