Skip to content

Commit 5bbef76

Browse files
author
Qiming Yuan
committed
Add support for app auth endpoints.
1 parent da65db3 commit 5bbef76

15 files changed

+373
-155
lines changed

Dropbox.Api.Tests/DropboxApiTests.cs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace Dropbox.Api.Tests
1616

1717
using Microsoft.VisualStudio.TestTools.UnitTesting;
1818

19+
using Dropbox.Api.Auth;
20+
1921
/// <summary>
2022
/// The test class for Dropbox API.
2123
/// </summary>
@@ -27,11 +29,29 @@ public class DropboxApiTests
2729
/// </summary>
2830
public static DropboxClient Client;
2931

32+
/// <summary>
33+
/// The Dropbox team client.
34+
/// </summary>
35+
public static DropboxTeamClient TeamClient;
36+
37+
/// <summary>
38+
/// The Dropbox app client.
39+
/// </summary>
40+
public static DropboxAppClient AppClient;
41+
3042
[ClassInitialize]
3143
public static void Initialize(TestContext context)
3244
{
33-
var token = context.Properties["accessToken"].ToString();
34-
Client = new DropboxClient(token);
45+
var userToken = context.Properties["userAccessToken"].ToString();
46+
Client = new DropboxClient(userToken);
47+
48+
var teamToken = context.Properties["teamAccessToken"].ToString();
49+
TeamClient = new DropboxTeamClient(teamToken);
50+
51+
var appKey = context.Properties["appKey"].ToString();
52+
var appSecret = context.Properties["appSecret"].ToString();
53+
54+
AppClient = new DropboxAppClient(appKey, appSecret);
3555
}
3656

3757

@@ -174,6 +194,61 @@ public async Task TestRequestId()
174194
}
175195
}
176196

197+
/// Test team auth.
198+
/// </summary>
199+
/// <returns>The <see cref="Task"/></returns>
200+
[TestMethod]
201+
public async Task TestTeamAuth()
202+
{
203+
var result = await TeamClient.Team.GetInfoAsync();
204+
Assert.IsNotNull(result.TeamId);
205+
Assert.IsNotNull(result.Name);
206+
}
207+
208+
/// Test team auth select user.
209+
/// </summary>
210+
/// <returns>The <see cref="Task"/></returns>
211+
[TestMethod]
212+
public async Task TestTeamAuthSelectUser()
213+
{
214+
var result = await TeamClient.Team.MembersListAsync();
215+
var memberId = result.Members[0].Profile.TeamMemberId;
216+
217+
var userClient = TeamClient.AsMember(memberId);
218+
var account = await userClient.Users.GetCurrentAccountAsync();
219+
Assert.AreEqual(account.TeamMemberId, memberId);
220+
}
221+
222+
/// Test app auth.
223+
/// </summary>
224+
/// <returns>The <see cref="Task"/></returns>
225+
[TestMethod]
226+
public async Task TestAppAuth()
227+
{
228+
try
229+
{
230+
var result = await AppClient.Auth.TokenFromOauth1Async("foo", "bar");
231+
}
232+
catch (ApiException<TokenFromOAuth1Error>)
233+
{
234+
}
235+
}
236+
237+
/// Test no auth.
238+
/// </summary>
239+
/// <returns>The <see cref="Task"/></returns>
240+
[TestMethod]
241+
public async Task TestNoAuth()
242+
{
243+
var result = await Client.Files.ListFolderAsync("", recursive: true);
244+
var cursor = result.Cursor;
245+
246+
var task = Client.Files.ListFolderLongpollAsync(cursor);
247+
await Client.Files.UploadAsync("/foo.txt", body: GetStream("abc"));
248+
var response = await task;
249+
Assert.IsTrue(response.Changes);
250+
}
251+
177252
/// <summary>
178253
/// Converts string to a memory stream.
179254
/// </summary>
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RunSettings>
33
<TestRunParameters>
4-
<Parameter name="accessToken" value="" />
4+
<Parameter name="userAccessToken" value="" />
5+
<Parameter name="teamAccessToken" value="" />
6+
<Parameter name="appKey" value="" />
7+
<Parameter name="appSecret" value="" />
58
</TestRunParameters>
69
</RunSettings>

Dropbox.Api/Auth/AuthAppRoutes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal AuthAppRoutes(enc.ITransport transport)
4141
/// cref="TokenFromOAuth1Error"/>.</exception>
4242
public t.Task<TokenFromOAuth1Result> TokenFromOauth1Async(TokenFromOAuth1Arg tokenFromOAuth1Arg)
4343
{
44-
return this.Transport.SendRpcRequestAsync<TokenFromOAuth1Arg, TokenFromOAuth1Result, TokenFromOAuth1Error>(tokenFromOAuth1Arg, "api", "/auth/token/from_oauth1", Dropbox.Api.Auth.TokenFromOAuth1Arg.Encoder, Dropbox.Api.Auth.TokenFromOAuth1Result.Decoder, Dropbox.Api.Auth.TokenFromOAuth1Error.Decoder);
44+
return this.Transport.SendRpcRequestAsync<TokenFromOAuth1Arg, TokenFromOAuth1Result, TokenFromOAuth1Error>(tokenFromOAuth1Arg, "api", "/auth/token/from_oauth1", "app", Dropbox.Api.Auth.TokenFromOAuth1Arg.Encoder, Dropbox.Api.Auth.TokenFromOAuth1Result.Decoder, Dropbox.Api.Auth.TokenFromOAuth1Error.Decoder);
4545
}
4646

4747
/// <summary>

Dropbox.Api/Auth/AuthUserRoutes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal AuthUserRoutes(enc.ITransport transport)
3535
/// <returns>The task that represents the asynchronous send operation.</returns>
3636
public t.Task TokenRevokeAsync()
3737
{
38-
return this.Transport.SendRpcRequestAsync<enc.Empty, enc.Empty, enc.Empty>(enc.Empty.Instance, "api", "/auth/token/revoke", enc.EmptyEncoder.Instance, enc.EmptyDecoder.Instance, enc.EmptyDecoder.Instance);
38+
return this.Transport.SendRpcRequestAsync<enc.Empty, enc.Empty, enc.Empty>(enc.Empty.Instance, "api", "/auth/token/revoke", "user", enc.EmptyEncoder.Instance, enc.EmptyDecoder.Instance, enc.EmptyDecoder.Instance);
3939
}
4040

4141
/// <summary>

Dropbox.Api/Dropbox.Api.Portable.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<Compile Include="Auth\TokenFromOAuth1Error.cs" />
8787
<Compile Include="Auth\TokenFromOAuth1Result.cs" />
8888
<Compile Include="AuthException.cs" />
89+
<Compile Include="DropboxAppClient.cs" />
8990
<Compile Include="DropboxClient.cs" />
9091
<Compile Include="DropboxTeamClient.cs" />
9192
<Compile Include="Files\AddPropertiesError.cs" />

Dropbox.Api/Dropbox.Api.Portable40.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
<Compile Include="Auth\TokenFromOAuth1Error.cs" />
111111
<Compile Include="Auth\TokenFromOAuth1Result.cs" />
112112
<Compile Include="AuthException.cs" />
113+
<Compile Include="DropboxAppClient.cs" />
113114
<Compile Include="DropboxClient.cs" />
114115
<Compile Include="DropboxTeamClient.cs" />
115116
<Compile Include="Files\AddPropertiesError.cs" />

Dropbox.Api/Dropbox.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<Compile Include="Auth\TokenFromOAuth1Error.cs" />
9292
<Compile Include="Auth\TokenFromOAuth1Result.cs" />
9393
<Compile Include="AuthException.cs" />
94+
<Compile Include="DropboxAppClient.cs" />
9495
<Compile Include="DropboxClient.cs" />
9596
<Compile Include="DropboxTeamClient.cs" />
9697
<Compile Include="Files\AddPropertiesError.cs" />

Dropbox.Api/DropboxAppClient.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// <auto-generated>
2+
// Auto-generated by StoneAPI, do not modify.
3+
// </auto-generated>
4+
5+
namespace Dropbox.Api
6+
{
7+
using sys = System;
8+
9+
using Dropbox.Api.Stone;
10+
using Dropbox.Api.Auth.Routes;
11+
12+
public sealed partial class DropboxAppClient
13+
{
14+
/// <summary>
15+
/// <para>Gets the Auth routes.</para>
16+
/// </summary>
17+
public AuthAppRoutes Auth { get; private set; }
18+
19+
/// <summary>
20+
/// <para>Initializes the routes.</para>
21+
/// </summary>
22+
/// <returns>The transport.</returns>
23+
private void InitializeRoutes(ITransport transport)
24+
{
25+
this.Auth = new AuthAppRoutes(transport);
26+
}
27+
}
28+
}

Dropbox.Api/DropboxClient.common.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,62 @@ public void Dispose()
135135
}
136136
}
137137

138+
/// <summary>
139+
/// The client which contains endpoints which perform app-auth actions.
140+
/// </summary>
141+
public sealed partial class DropboxAppClient
142+
{
143+
/// <summary>
144+
/// Initializes a new instance of the <see cref="T:Dropbox.Api.DropboxAppClient"/> class.
145+
/// </summary>
146+
/// <param name="appKey">The Dropbox app key (e.g. consumer key in OAuth).</param>
147+
/// <param name="appSecret">The Dropbox app secret (e.g. consumer secret in OAuth).</param>
148+
public DropboxAppClient(string appKey, string appSecret)
149+
: this(appKey, appSecret, new DropboxClientConfig())
150+
{
151+
}
152+
153+
154+
/// <summary>
155+
/// Initializes a new instance of the <see cref="T:Dropbox.Api.DropboxClient"/> class.
156+
/// </summary>
157+
/// <param name="appKey">The Dropbox app key (e.g. consumer key in OAuth).</param>
158+
/// <param name="appSecret">The Dropbox app secret (e.g. consumer secret in OAuth).</param>
159+
/// <param name="config">The <see cref="DropboxClientConfig"/>.</param>
160+
public DropboxAppClient(string appKey, string appSecret, DropboxClientConfig config)
161+
{
162+
if (appKey == null)
163+
{
164+
throw new ArgumentNullException("appKey");
165+
}
166+
167+
if (appSecret == null)
168+
{
169+
throw new ArgumentNullException("appSecret");
170+
}
171+
172+
var options = new DropboxRequestHandlerOptions(
173+
GetBasicAuthHeader(appKey, appSecret),
174+
config.MaxRetriesOnError,
175+
config.UserAgent,
176+
httpClient: config.HttpClient);
177+
178+
this.InitializeRoutes(new DropboxRequestHandler(options));
179+
}
180+
181+
/// <summary>
182+
/// Gets the basic auth header from app key and app secret.
183+
/// </summary>
184+
/// <param name="appKey">The app key.</param>
185+
/// <param name="appSecret">The app secret.</param>
186+
/// <returns>The basic auth header.</returns>
187+
private static string GetBasicAuthHeader(string appKey, string appSecret)
188+
{
189+
var rawValue = string.Format("{0}:{1}", appKey, appSecret);
190+
return Convert.ToBase64String(Encoding.UTF8.GetBytes(rawValue));
191+
}
192+
}
193+
138194
/// <summary>
139195
/// The client which contains endpoints which perform team-level actions.
140196
/// </summary>

0 commit comments

Comments
 (0)