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