【问题标题】:Grabbing Dropbox access token on Windows Form using Dropbox API使用 Dropbox API 在 Windows 窗体上获取 Dropbox 访问令牌
【发布时间】:2017-02-07 14:03:05
【问题描述】:

我已经完成了一个已经使用 Dropbox API 上传文件、下载、删除等的类。自从我只使用自己的访问令牌以来,它一直运行良好,但我需要注册其他用户,并且出现了一个但“大”的问题:检索访问令牌

1.- 重定向 URI?我开始怀疑我为什么需要这个。我最终使用了这个 URI (https://www.dropbox.com/1/oauth2/redirect_receiver),因为 "The redirect URI you use doesn't really matter" 当然,我在 Dropbox 上的应用配置中包含了这个。

2.- 我访问了用户的帐户(我可以看到用户的数量增加了,并且我看到应用可以访问用户的帐户。

3.- 我的代码上有一个断点来检查变量以应用 DropboxOAuth2Helper.ParseTokenFragment 但我在那里没有成功。

这是我的代码,但是在 try catch 之前的 if 上是卡住的地方:

string AccessToken;
const string AppKey = "theOneAtmyAppConfigOnDropbox";
const string redirectUrl = "https://www.dropbox.com/1/oauth2/redirect_receiver";
string oauthUrl =
    $@"https://www.dropbox.com/1/oauth2/authorize?response_type=token&redirect_uri={redirectUrl}&client_id={AppKey}";

private string oauth2State;
private bool Result;

public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    Start(AppKey, webBrowser1);

    webBrowser1.Navigating += Browser_Navigating;
}

private void Start(string appKey, WebBrowser w)
{
    this.oauth2State = Guid.NewGuid().ToString("N");
    Uri authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(OauthResponseType.Token, appKey, redirectUrl, state: oauth2State);
    w.Navigate(authorizeUri);
}

private void Browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    if (!e.Url.ToString().StartsWith(redirectUrl, StringComparison.InvariantCultureIgnoreCase))
    {

        // we need to ignore all navigation that isn't to the redirect uri.
        return;
    }

    try
    {
        OAuth2Response result = DropboxOAuth2Helper.ParseTokenFragment(e.Url);
        if (result.State != this.oauth2State)
        {
            // The state in the response doesn't match the state in the request.
            return;
        }

        this.AccessToken = result.AccessToken;
        this.Result = true;
    }
    catch (ArgumentException)
    {
        // There was an error in the URI passed to ParseTokenFragment
    }
    finally
    {
        e.Cancel = true;
        this.Close();
    }
}

我已经为此抗争了好几个小时,但我现在开始看到事情有点模糊。

This 是我使用的教程,但我没有继续前进。非常感谢任何帮助!

编辑:我终于向前迈出了一步。我更改了包含

的行
Uri authorizeUri2 = DropboxOAuth2Helper.GetAuthorizeUri(appKey);

现在我在 WebClient 上显示生成的访问令牌!尝试获取它时会出现不好的部分(它进入 if 中),并且每次我向用户请求许可时都会生成它,因此它会被覆盖。

编辑 2:我注意到我在浏览器上生成的令牌格式不正确。我在调试时尝试手动对其进行硬核更改,并且在创建 DropboxClient 对象时出现 AuthException 时出现异常:( 什么鬼!

【问题讨论】:

标签: c# .net oauth-2.0 dropbox-api


【解决方案1】:

正如 Greg 所说,解决方案是使用事件 Browser_Navigated。看起来我的 Visual Studio (2015) 使用的嵌入式 IE 版本没有注意到,如果它是重定向,它不会启动事件 BrowserNavigating。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    相关资源
    最近更新 更多