简体   繁体   English

使用Dropnet获取Dropbox访问令牌

[英]Getting Dropbox Access Token With Dropnet

I'm attempting to implement file uploading to Dropbox on my site. 我正在尝试将文件上传到我网站上的Dropbox。 However, I'm having trouble getting the accessToken after the user clicks to authorize my app. 但是,在用户单击以授权我的应用后,我很难获得accessToken。

Here is my code to grab the URL, which gets returned to the client to open a new window in Javascript. 这是我获取URL的代码,该URL返回给客户端以使用Javascript打开新窗口。

[WebMethod]
public String setUpDropboxOA(String uri, Int32 form_id, String user_auth)
{
    var client = new DropNetClient("xxxxxxxxx", "xxxxxxxxx");
    return client.GetTokenAndBuildUrl(uri);
}

And here is my callback function: 这是我的回调函数:

[WebMethod]
public void AuthorizeDropboxCallback(String oauth_token)
{
    var client = new DropNetClient("xxxxxxxxx", "xxxxxxxxx");
    var accessToken = client.GetAccessToken();
    var jsonObj = new { oauth_token = accessToken.Token, oauth_secret = accessToken.Secret };
    var JSONAuthorizationData = JsonConvert.SerializeObject(jsonObj);
    saveNotification(form_hash, "Dropbox", JSONAuthorizationData, user_id);
}

And here is the error that I'm getting on client.GetAccessToken(): 这是我在client.GetAccessToken()上遇到的错误:

Exception of type 'DropNet.Exceptions.DropboxException' was thrown.

The documentation of DropNet says that there is an overload to GetAccessToken that will allow you to specify a token to use, however, I'm not seeing one. DropNet的文档说GetAccessToken有一个重载,它允许您指定要使用的令牌,但是,我没有看到。 I feel like this is the problem here, but I'm not entirely sure. 我觉得这就是问题所在,但我不确定。

As @albattran's answer suggested it is because you are creating 2 different instances of the DropNetClient. 正如@albattran的答案所暗示的,这是因为您要创建两个不同的DropNetClient实例。

client.GetTokenAndBuildUrl(uri);

Thismethod actually does 2 things under the hood. 该方法实际上是在后台执行2件事。 1, Makes an API call to Dropbox to get a request token then using that request token it creates the login url. 1,对Dropbox进行API调用以获取请求令牌,然后使用该请求令牌创建登录URL。

To solve this you will need a way to store that request token between web requests. 为了解决这个问题,您将需要一种在Web请求之间存储该请求令牌的方法。

Maybe think about something like the below using the session. 也许使用该会话考虑以下内容。

var userToken = client.GetToken();
Session["user_token"] = userToken.Token;
Session["user_secret"] = userToken.Secret;

Then ok the callback read those session variables and add them to the constructor overload of the DropNetClient. 然后,确定回调,读取这些会话变量,并将其添加到DropNetClient的构造函数重载中。

var token = Session["user_token"];
var secret = Session["user_secret"];
var client = new DropNetClient("XXXX", "XXXX", token, secret);
client.GetAccessToken();

I think your problem is a result of losing the instance of DropNetClient between the different requests, you are creating two instances of DropNetClient. 我认为您的问题是在不同请求之间丢失了DropNetClient实例的结果,您正在创建两个DropNetClient实例。

You need to persist the initial token form GetTokenAndBuildUrl and restore it when you call GetAccessToken. 您需要保留初始令牌形式GetTokenAndBuildUrl并在调用GetAccessToken时将其还原。

Because oAuth is 3 steps: 由于oAuth是3个步骤:

  1. Get Request Token 获取请求令牌
  2. Send user for authorization, and get back verifier 向用户发送授权,然后返回验证者
  3. Get Access token using the original Request Token and the verifier 使用原始请求令牌和验证程序获取访问令牌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM