dropNet 的 DropNetClient.GetAccessToken 错误(未处理的异常)
Error in dropNet's DropNetClient.GetAccessToken (Unhandled exception)
我正在 .Net 中制作一个 dropBox 下载器,每次我尝试 运行 代码时都会遇到未处理的异常。我已经验证了保管箱应用程序。
代码如下:
dropClient = new DropNetClient("xxxxxxxxxxxxx", "yyyyyyyyyyyyyy");
//Btw in my code I have the actual app key and secret
dropClient.GetToken();
var accessToken = dropClient.GetAccessToken();
dropClient.UserLogin = new DropNet.Models.UserLogin { Token = "myemail@gmail.com", Secret = "myPass" };
这里的问题是您实际上并没有获得客户端凭据
这是我的 dropnet 连接管理器 class
public class ConnectionManager
{
private DropNetClient _client;
public string GetConnectUrl(DropNetClient client, string callbackurl)
{
_client = client;
var url = _client.BuildAuthorizeUrl(callbackurl);
return url;
}
public DropNetClient Connect()
{
_client = new DropNetClient("token", "secret");
_client.GetToken();
return _client;
}
public Dictionary<string, string> GetAccessToken(string tok, string secret)
{
_client = new DropNetClient("token", "secret", tok, secret);
var token = _client.GetAccessToken();
var dic = new Dictionary<string, string> {{"token", token.Token}, {"secret", token.Secret}};
return dic;
}
public DropNetClient Connect(TokenAndSecretModel model)
{
_client = new DropNetClient("token", "secret", model.Token, model.Secret);
var info = _client.AccountInfo();
return _client;
}
}
首先调用Connect()
然后 GetConnectUrl(...) 您的回调 url 需要知道调用何时完成。
重定向到 URL returned,然后等待您的回调 url 收到响应。当它确实调用 GetAccessToken(token, secret) 时,令牌和秘密来自 _client.UserLogin.Token 和 _client.UserLogin.Secret
然后 GetAccessToken return 您需要保存的 Token 和 Secret
为了完成这个,这是我的控制器
public class DropBoxController : Controller
{
private readonly ICommandChannel _commandChannel;
private readonly IQueryChannel _queryChannel;
private readonly UserModel _user;
public DropBoxController(ICommandChannel commandChannel, IQueryChannel queryChannel, UserModel user)
{
_commandChannel = commandChannel;
_queryChannel = queryChannel;
_user = user;
}
public ActionResult Index()
{
var con = new ConnectionManager();
var dropclient = con.Connect();
var callbackurl = Request.Url.Scheme + "://" + Request.Url.Authority + "/DropBox/Callback";
var url = con.GetConnectUrl(dropclient, callbackurl);
_commandChannel.Execute(new SaveDropBoxTempSecurityCommand { AuthToken = dropclient.UserLogin.Token, Token = dropclient.UserLogin.Token, Secret = dropclient.UserLogin.Secret });
return Redirect(url);
//return View(new UrlModel {Url = url});
}
[HttpGet]
public ActionResult Callback(string oauth_token)
{
TokenAndSecretModel model = _queryChannel.Execute(new GetDropBoxTempTokenQuery{Token = oauth_token});
var con = new ConnectionManager();
var login = con.GetAccessToken(model.Token, model.Secret);
_commandChannel.Execute(new SaveDropBoxLoginCommand{AuthToken = oauth_token, Login = login});
return View();
}
}
public class UrlModel
{
public string Url { get; set; }
}
你可以看到我将临时凭据保存到数据库中,如果你想将它们保存到会话中也可以。希望对您有所帮助。
我正在 .Net 中制作一个 dropBox 下载器,每次我尝试 运行 代码时都会遇到未处理的异常。我已经验证了保管箱应用程序。
代码如下:
dropClient = new DropNetClient("xxxxxxxxxxxxx", "yyyyyyyyyyyyyy");
//Btw in my code I have the actual app key and secret
dropClient.GetToken();
var accessToken = dropClient.GetAccessToken();
dropClient.UserLogin = new DropNet.Models.UserLogin { Token = "myemail@gmail.com", Secret = "myPass" };
这里的问题是您实际上并没有获得客户端凭据
这是我的 dropnet 连接管理器 class
public class ConnectionManager
{
private DropNetClient _client;
public string GetConnectUrl(DropNetClient client, string callbackurl)
{
_client = client;
var url = _client.BuildAuthorizeUrl(callbackurl);
return url;
}
public DropNetClient Connect()
{
_client = new DropNetClient("token", "secret");
_client.GetToken();
return _client;
}
public Dictionary<string, string> GetAccessToken(string tok, string secret)
{
_client = new DropNetClient("token", "secret", tok, secret);
var token = _client.GetAccessToken();
var dic = new Dictionary<string, string> {{"token", token.Token}, {"secret", token.Secret}};
return dic;
}
public DropNetClient Connect(TokenAndSecretModel model)
{
_client = new DropNetClient("token", "secret", model.Token, model.Secret);
var info = _client.AccountInfo();
return _client;
}
}
首先调用Connect() 然后 GetConnectUrl(...) 您的回调 url 需要知道调用何时完成。 重定向到 URL returned,然后等待您的回调 url 收到响应。当它确实调用 GetAccessToken(token, secret) 时,令牌和秘密来自 _client.UserLogin.Token 和 _client.UserLogin.Secret 然后 GetAccessToken return 您需要保存的 Token 和 Secret
为了完成这个,这是我的控制器
public class DropBoxController : Controller
{
private readonly ICommandChannel _commandChannel;
private readonly IQueryChannel _queryChannel;
private readonly UserModel _user;
public DropBoxController(ICommandChannel commandChannel, IQueryChannel queryChannel, UserModel user)
{
_commandChannel = commandChannel;
_queryChannel = queryChannel;
_user = user;
}
public ActionResult Index()
{
var con = new ConnectionManager();
var dropclient = con.Connect();
var callbackurl = Request.Url.Scheme + "://" + Request.Url.Authority + "/DropBox/Callback";
var url = con.GetConnectUrl(dropclient, callbackurl);
_commandChannel.Execute(new SaveDropBoxTempSecurityCommand { AuthToken = dropclient.UserLogin.Token, Token = dropclient.UserLogin.Token, Secret = dropclient.UserLogin.Secret });
return Redirect(url);
//return View(new UrlModel {Url = url});
}
[HttpGet]
public ActionResult Callback(string oauth_token)
{
TokenAndSecretModel model = _queryChannel.Execute(new GetDropBoxTempTokenQuery{Token = oauth_token});
var con = new ConnectionManager();
var login = con.GetAccessToken(model.Token, model.Secret);
_commandChannel.Execute(new SaveDropBoxLoginCommand{AuthToken = oauth_token, Login = login});
return View();
}
}
public class UrlModel
{
public string Url { get; set; }
}
你可以看到我将临时凭据保存到数据库中,如果你想将它们保存到会话中也可以。希望对您有所帮助。