如何获取Box的刷新令牌

时间:2014-03-06 08:47:04

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

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

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

2 个答案:

答案 0 :(得分:4)

为了使用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")

每次刷新Refresh令牌时,都会向您提供一个新的Access令牌。 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。对不起,现在还没有说清楚!

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

答案 1 :(得分:0)

使用此代码

/*******************/
/** 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