2015-09-05 82 views
0

在试图授权C#应用程序来访问电子表格,我也碰到过这样的错误:谷歌OATH2电子表格错误:找不到指定文件

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

回答

0

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