gpt4 book ai didi

c# - DropNet 无法为后续请求保存访问 token ?

转载 作者:太空宇宙 更新时间:2023-11-03 10:32:24 25 4
gpt4 key购买 nike

和我之前的许多人一样,我在应用程序启动之间成功保存访问 token 失败得很惨。该应用程序实际上只会与一个特定的保管箱帐户一起使用,尽管我正在努力使其易于设置且更具动态性。

当然,如果设置为空(初始运行),用户可以正确登录并授权该应用程序,然后返回到它按预期运行的应用程序。然而,在随后的运行中,当它从设置集合中获取 token 和 secret 时,它会悲惨地失败并显示

Received Response [Unauthorized] : Expected to see [OK]. The HTTP response was [{"error": "Invalid signature."}].

我显然做错了什么,这是什么?谢谢!

下面的代码!

using System;
using DropNet;

namespace DS_Uploader_DropBox {
class Program {
private const string AppKey = "my super secret app key";
private const string AppSecret = "my super secret app secret";

static void Main(string[] args) {
DropNetClient client;
DropNet.Models.UserLogin token;

string userToken = Settings.Default.userToken;
string userSecret = Settings.Default.userSecret;

bool needAccessToken = (String.IsNullOrEmpty(userToken) || string.IsNullOrEmpty(userSecret));

//needAccessToken = true;

if (needAccessToken) {
client = new DropNet.DropNetClient(AppKey, AppSecret);
client.UseSandbox = true;
client.GetToken();

// Auth with dropbox
var url = client.BuildAuthorizeUrl();

// Prompt for user to auth
Console.WriteLine("go auth here " + url);
Console.ReadLine();

// If the user authed, let's get that token
try {
token = client.GetAccessToken();
}
catch (Exception e) {
Console.WriteLine("Exception! " + e.Message);
return;
}

// save for later
userToken = token.Token;
userSecret = token.Secret;
Settings.Default.userToken = userToken;
Settings.Default.userSecret = userSecret;
Settings.Default.Save();

} else {
client = new DropNet.DropNetClient(AppKey, AppSecret, userToken, userSecret);
client.UseSandbox = true;
client.GetToken();

// get that token
try {
token = client.GetAccessToken();
} catch (Exception e) {
Console.WriteLine("Exception! " + e.Message);
return;
}
}

var acctInfo = client.AccountInfo();
Console.WriteLine(acctInfo.display_name);
Console.ReadLine();
}
}
}

可遵循的代码:

using System;
using DropNet;

namespace DS_Uploader_DropBox {
class Program {
private const string AppKey = "my super secret app key";
private const string AppSecret = "my super secret app secret";

static void Main(string[] args) {
DropNetClient client;
DropNet.Models.UserLogin token;

string userToken = Settings.Default.userToken;
string userSecret = Settings.Default.userSecret;

bool needAccessToken = (String.IsNullOrEmpty(userToken) || string.IsNullOrEmpty(userSecret));

//needAccessToken = true;

if (needAccessToken) {
client = new DropNet.DropNetClient(AppKey, AppSecret);
client.UseSandbox = true;
client.GetToken();

// Auth with dropbox
var url = client.BuildAuthorizeUrl();

// Prompt for user to auth
Console.WriteLine("go auth here " + url);
Console.ReadLine();

// If the user authed, let's get that token
try {
token = client.GetAccessToken();
}
catch (Exception e) {
Console.WriteLine("Exception! " + e.Message);
return;
}

// save for later
userToken = token.Token;
userSecret = token.Secret;
Settings.Default.userToken = userToken;
Settings.Default.userSecret = userSecret;
Settings.Default.Save();

} else {
client = new DropNet.DropNetClient(AppKey, AppSecret, userToken, userSecret);
client.UseSandbox = true;
}

var acctInfo = client.AccountInfo();
Console.WriteLine(acctInfo.display_name);
Console.ReadLine();
}
}
}

最佳答案

needAccessToken 为 false 的代码路径中,您再次调用 GetTokenGetAccessToken 以尝试获取新请求 token 和新的访问 token 。这是不必要的,因为您已经在 userTokenuserSecret 中拥有并检索了现有的访问 token 。

关于c# - DropNet 无法为后续请求保存访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29520473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com