这里的问题是您实际上并没有获得客户端凭据
这是我的 dropnet 连接管理器类
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,然后等待您的回调 url 收到响应。当它确实调用 GetAccessToken(token, secret) 时,令牌和机密来自 _client.UserLogin.Token 和 _client.UserLogin.Secret GetAccessToken 然后将返回您需要保存的令牌和机密
为了完成这个,这是我的控制器
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; }
}
您可以看到我将临时凭证保存到数据库中,如果您只想将它们保存到会话中,您可以。我希望这有帮助。