如何在Android上实现OAuth2授权
我需要在我的应用程序中添加OAuth2授权。我只有客户id、客户机密和用户名(电子邮件)。我需要一个代币。你能告诉我怎么做吗?库还是示例代码 您可以使用来执行OAuth2授权 有关示例,请参见如何在Android上实现OAuth2授权,android,oauth-2.0,Android,Oauth 2.0,我需要在我的应用程序中添加OAuth2授权。我只有客户id、客户机密和用户名(电子邮件)。我需要一个代币。你能告诉我怎么做吗?库还是示例代码 您可以使用来执行OAuth2授权 有关示例,请参见 以下是AppAuth文档的简短版本 概述 建议本机应用程序使用授权代码流 该流程有效地由四个阶段组成: 指定授权服务配置 通过浏览器进行授权,以获取授权代码 交换授权码,以获取访问和刷新令牌 使用访问令牌访问受保护的资源服务 1。创建授权服务配置 首先,创建授权服务的配置,该配置将在第二和第三阶段中使用
以下是AppAuth文档的简短版本 概述 建议本机应用程序使用授权代码流 该流程有效地由四个阶段组成:
AuthorizationServiceConfiguration mServiceConfiguration =
new AuthorizationServiceConfiguration(
Uri.parse("https://example.com/authorize"), // Authorization endpoint
Uri.parse("https://example.com/token")); // Token endpoint
ClientAuthentication mClientAuthentication =
new ClientSecretBasic("my-client-secret"); // Client secret
(本机应用程序中不建议使用静态客户端机密。)
2。请求授权并获取授权代码
private void retrieveTokens(AuthorizationResponse authResponse) {
TokenRequest tokenRequest = response.createTokenExchangeRequest();
AuthorizationService service = new AuthorizationService(this);
service.performTokenRequest(request, mClientAuthentication,
new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(TokenResponse tokenResponse,
AuthorizationException tokenException) {
mAuthState.update(tokenResponse, tokenException);
// Handle token response error here
persistAuthState(mAuthState);
}
});
}
要接收授权回调,请在清单文件中定义以下活动。(您不需要实施此活动。此活动将充当您授权请求的代理。)
处理授权响应
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode != REQUEST_CODE_AUTH) {
return;
}
AuthorizationResponse authResponse = AuthorizationResponse.fromIntent(intent);
AuthorizationException authException = AuthorizationException.fromIntent(intent);
mAuthState = new AuthState(authResponse, authException);
// Handle authorization response error here
retrieveTokens(authResponse);
}
3。交换授权码
private void retrieveTokens(AuthorizationResponse authResponse) {
TokenRequest tokenRequest = response.createTokenExchangeRequest();
AuthorizationService service = new AuthorizationService(this);
service.performTokenRequest(request, mClientAuthentication,
new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(TokenResponse tokenResponse,
AuthorizationException tokenException) {
mAuthState.update(tokenResponse, tokenException);
// Handle token response error here
persistAuthState(mAuthState);
}
});
}
令牌检索成功完成后,请保持AuthState
,以便您可以在下一个应用程序(重新)启动时重新使用它
4。访问受保护的资源服务
private void retrieveTokens(AuthorizationResponse authResponse) {
TokenRequest tokenRequest = response.createTokenExchangeRequest();
AuthorizationService service = new AuthorizationService(this);
service.performTokenRequest(request, mClientAuthentication,
new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(TokenResponse tokenResponse,
AuthorizationException tokenException) {
mAuthState.update(tokenResponse, tokenException);
// Handle token response error here
persistAuthState(mAuthState);
}
});
}
使用performationwithfreshtokens
使用新的访问令牌执行API调用。(它将自动确保令牌是新的,并在需要时刷新它们。)
执行API调用。(使用AsyncTask
只是为了简单。它可能不是执行API调用的最佳解决方案。)
private void executeApiCall(字符串访问令牌){
新建异步任务(){
@凌驾
受保护的字符串doInBackground(字符串…参数){
OkHttpClient=新的OkHttpClient();
Request Request=newrequest.Builder()
.url(“https://example.com/api/...“”//API URL
.addHeader(“授权”,
格式(“承载%s”,参数[0]))
.build();
试一试{
Response=client.newCall(request.execute();
返回响应.body().string();
}捕获(例外e){
//在这里处理API错误
}
}
@凌驾
受保护的void onPostExecute(字符串响应){
...
}
}.执行(accessToken);
}
这是Android的推荐方法吗?我曾经认为移动设备可以使用设备的默认web浏览器作为用户代理来处理代码和令牌重定向。最近有没有变化?AppAuth是谷歌推荐的。它使用Chrome自定义选项卡进行授权请求。与设备的默认web浏览器相比,Chrome自定义选项卡具有一些优势。例如,“自定义”选项卡与最后显示的活动重叠。(请参阅以下来自谷歌的谈话:)
private void prepareApiCall() {
AuthorizationService service = new AuthorizationService(this);
mAuthState.performActionWithFreshTokens(service, mClientAuthentication,
new AuthState.AuthStateAction() {
@Override
public void execute(String accessToken, String idToken,
AuthorizationException authException) {
// Handle token refresh error here
executeApiCall(accessToken);
}
});
}
private void executeApiCall(String accessToken) {
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://example.com/api/...") // API URL
.addHeader("Authorization",
String.format("Bearer %s", params[0]))
.build();
try {
Response response = client.newCall(request).execute();
return response.body().string();
} catch (Exception e) {
// Handle API error here
}
}
@Override
protected void onPostExecute(String response) {
...
}
}.execute(accessToken);
}