Repository navigation
Expand file tree
/
Copy pathDbxWebAuthHelper.java
More file actions
54 lines (46 loc) · 2.19 KB
/
Copy pathDbxWebAuthHelper.java
File metadata and controls
54 lines (46 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.dropbox.core;
import com.dropbox.core.http.HttpRequestor;
import com.dropbox.core.util.StringUtil;
import java.util.ArrayList;
/*>>> import checkers.nullness.quals.Nullable; */
/*>>> import checkers.nullness.quals.NonNull; */
abstract class DbxWebAuthHelper
{
public static String getAuthorizeUrl(DbxAppInfo appInfo, /*@Nullable*/String userLocale,
/*@Nullable*/String redirectUri, /*@Nullable*/String state)
{
return DbxRequestUtil.buildUrlWithParams(userLocale,
appInfo.host.web, "1/oauth2/authorize", new String[] {
"client_id", appInfo.key,
"response_type", "code",
"redirect_uri", redirectUri,
"state", state,
});
}
public static DbxAuthFinish finish(DbxAppInfo appInfo, DbxRequestConfig requestConfig,
String code, /*@Nullable*/String originalRedirectUri)
throws DbxException
{
if (code == null) throw new IllegalArgumentException("'code' can't be null");
/*@Nullable*/String[] params = {
"grant_type", "authorization_code",
"code", code,
"redirect_uri", originalRedirectUri,
"locale", requestConfig.userLocale,
};
ArrayList<HttpRequestor.Header> headers = new ArrayList<HttpRequestor.Header>();
String credentials = appInfo.key + ":" + appInfo.secret;
String base64Credentials = StringUtil.base64Encode(StringUtil.stringToUtf8(credentials));
headers.add(new HttpRequestor.Header("Authorization", "Basic " + base64Credentials));
return DbxRequestUtil.doPostNoAuth(requestConfig, appInfo.host.api, "1/oauth2/token",
params, headers, new DbxRequestUtil.ResponseHandler<DbxAuthFinish>()
{
@Override
public DbxAuthFinish handle(HttpRequestor.Response response) throws DbxException
{
if (response.statusCode != 200) throw DbxRequestUtil.unexpectedStatus(response);
return DbxRequestUtil.readJsonFromResponse(DbxAuthFinish.Reader, response.body);
}
});
}
}