【问题标题】:How to get refresh token of Box如何获取 Box 的刷新令牌
【发布时间】:2014-04-08 18:46:42
【问题描述】:

我有一个将文件上传到 Box 的 C# 桌面应用程序, 它需要一个刷新令牌来生成访问令牌,但我无法以编程方式创建刷新令牌。从This site 创建刷新令牌并成功将文件上传到 Box,但此刷新令牌在几分钟后过期并引发异常Refresh token has expired。我正在使用box-csharp-sdk-v2

所以我想使用我的客户端详细信息或 api 密钥以编程方式创建一个刷新令牌

【问题讨论】:

    标签: c# .net upload box-api file-sharing


    【解决方案1】:

    为了使用 Box OAuth2 令牌,您需要使用您的应用程序的 API 密钥和密钥来实例化 TokenProvider

    // Instantiate a token provider.
    var tokenProvider = new TokenProvider("apiKey", "apiSecret");
    

    通过 OAuth2 工作流程发送用户后,您将收到授权 codeTokenProvider 可以使用这个code 来获取第一个访问/刷新令牌对:

    // Fetch the initial access/refresh token pair
    // You will want to persist these new values for later use.
    var initialTokenPair = tokenProvider.GetAccessToken("code")
    

    每次刷新Access 令牌时,都会为您提供一个新的Refresh 令牌。 Refresh 令牌在使用时或大约 60 天后过期,以先到者为准。

    // Refresh the token pair.
    // You will want to persist these new values for later use.
    var newTokenPair = tokenProvider.RefreshAccessToken("refreshToken");
    

    我用上面的例子更新了 box-csharp-sdk-v2 readme。很抱歉之前没有说清楚!

    编辑:添加了获取初始令牌对的示例。

    【讨论】:

    • 感谢您的回答,当我获得刷新令牌时,我能够生成访问令牌并保存以供将来使用,并且使用此访问令牌我可以上传文件,但我怎么能第一次以编程方式生成刷新令牌??
    • 当我将文件上传到盒子并在下次病房删除时,上传的文件将仅显示在垃圾文件夹中,我尝试使用不同的文件夹名称和文件名
    • 如何以编程方式第一次生成刷新令牌?我添加了一个使用 Box 提供的 OAuth2 授权 code 获取初始令牌对的示例。
    • 我正在使用使用 c# 开发的 Windows 应用程序,“box.com/api/oauth2/…”带有这个 url 我提出了一个获取请求,但我没有得到“CODE”。我还在我的box 帐户是localhost:8085/1,我创建了 ac# webserver 来监听端口 8085,但我没有收到任何关于代码的请求。我是 BOX 的新手,请解释一下获取“CODE”的基本步骤" 来自 ac# windows 应用程序?
    • “通过 OAuth2 工作流程发送用户后”是什么意思?这是否意味着将提示用户输入身份验证?如果是这样,我想有人可能会称之为程序访问,但这不是我通常认为的程序访问。但也许你已经回答了问题的信。我假设OP正在询问自动化;没有用户交互。而且,自动化是我感兴趣的。
    【解决方案2】:

    使用此代码

    /*******************/
    /** Authorization **/
    /*******************/
    
    // Create a OAuth2 access/refresh token provider using your API key/secret.
    var tokenProvider = new TokenProvider("apiKey", "apiSecret");
    
    // Fetch the initial token pair using the OAuth2 authorization code...
    // You will want to persist these new values for later use.
    var initialTokenPair = tokenProvider.GetAccessToken("code");
    
    // You can also refresh the token pair.
    // You will want to persist these new values for later use.
    var newTokenPair = tokenProvider.RefreshAccessToken("refreshToken");
    
    /*********************/
    /** Box Interaction **/
    /*********************/
    
    // Instantiate a BoxManager client.
    var boxManager = new BoxManager(newTokenPair.AccessToken);
    
    // Create a new file in the root folder
    boxManager.CreateFile(Folder.Root, "a new file.txt", Encoding.UTF8.GetBytes("hello, world!"));
    
    // Fetch the root folder
    var folder = boxManager.GetFolder(Folder.Root);
    
    // Find a 'mini' representation of the created file among the root folder's contents
    var file = folder.Files.Single(f => f.Name.Equals("a new file.txt"));
    
    // Get the file with all properties populated.
    file = boxManager.Get(file);
    
    // Rename the file
    file = boxManager.Rename(file, "the new name.txt");
    
    // Create a new subfolder
    var subfolder = boxManager.CreateFolder(Folder.Root, "my subfolder");
    
    // Move the file to the subfolder
    file = boxManager.Move(file, subfolder);
    
    // Write some content to the file
    using (var stream = new MemoryStream(Encoding.UTF8.GetBytes("goodbye, world!")))
    {
        file = boxManager.Write(file, stream);
    }
    
    // Read the contents to a stream and write them to the console
    using (var stream = new MemoryStream())
    {
        boxManager.Read(file, stream);
        using (var reader = new StreamReader(stream))
        {
            stream.Position = 0;
            Console.Out.WriteLine("File content: '{0}'", reader.ReadToEnd());
        }
    }
    
    // Delete the folder and its contents
    boxManager.Delete(subfolder, recursive: true);
    
    As an enterprise administrator you can create a client and perform Box operations on behalf of another user.
    
    // Instantiate a BoxManager client.
    var boxManager = new BoxManager("AccessToken", onBehalfOf: "user@domain.com");
    
    // ... do stuff as that user
    // ... use your power only for awesome!
    

    Github

    【讨论】:

      猜你喜欢
      • 2019-05-02
      • 2021-08-25
      • 2018-02-27
      • 1970-01-01
      • 2020-02-26
      • 2019-09-09
      • 2012-05-04
      • 2012-06-29
      相关资源
      最近更新 更多