RESTful Api android 应用的身份验证
RESTful Api authentication for an android app
我有一个作业需要使用电子邮件和密码来验证用户并获取访问令牌。我同时拥有 api 密钥、秘密和基础 URL。我不需要为作业使用重定向 URL,也没有提供。我不确定要使用哪种方法或哪个库。我淹没在大量的信息中,这让我感到困惑。我需要指出正确的方向......欢迎任何形式的帮助。谢谢
我建议您使用 retrofit 库来做到这一点。
假设您的 URL 基础是 http://baseurl.com/api,您必须执行 GET 请求到 /login 并传递电子邮件和密码。我假设您的 API 将 return 一个用户对象作为 JSON.
Api.java
public interface Api {
@GET("/login")
public void login(@Query("email") String email, @Query("password"), Callback<User> callback);
}
需要执行 API 调用的位置:
Retrofit retrofit = new Retrofit.Builder()
.setEndpoint("http://baseurl.com")
.build();
Api api = retrofit.create(Api.class);
api.login(email, password, new Callback<User>() {
@Override
public void success(User user, Response response) {
// login logic
}
@Override
public void failure(RetrofitError error) {
Log.e("Retrofit", error.getMessage());
}
});
希望这个例子能帮到你。不要忘记阅读 retrofit documentation
根据您的评论,说明告诉您在规范中使用 Resource Owner Password Credentials Grant. You can see an example request。
POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=johndoe&password=A3ddj3w
唯一可能看起来很奇怪的事情(如果您从未遇到过)是 Authorization
header 值。继续阅读 Basic Authentication。基本上 czZCaGRSa3F0MzpnWDFmQmF0M2JW
是 username:password
的 base64 编码(实际上是 <client_id>:<client_secret>
)。
在不使用任何外部库(只是标准 Java 库)的情况下发出请求,您可能会有类似
String formData = "username=<uname>&password=<pass>&grant_type=password";
String header = "Basic " + Base64.encodeAsString("<client_id>:<client_secret>");
HttpURLConnection connection
= (HttpURLConnection) new URL(tokenUrl).openConnection();
connection.setDoOutput(true);
connection.addRequestProperty("Authorization", header);
connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", Integer.toString(formData.length()));
OutputStream out = connection.getOutputStream();
out.write(formData.getBytes(StandardCharsets.UTF_8));
InputStream in = connection.getInputStream();
AccessToken token = new ObjectMapper().readValue(in, AccessToken.class);
System.out.println(token);
out.close();
in.close();
我用的Base64
不是标准库class。 ObjectMapper
也不是标准库 class。我只是用它来解析对 AccessToken
class 的令牌响应。您可以使用任何您喜欢的解析器。 AccessToken
class 只包含所有可能的标记值
public class AccessToken {
public String access_token;
public String refresh_token;
public long expires_in;
public String token_type;
public String scope;
}
从那里开始,一旦你有了令牌,你想发出任何资源请求,你只需要添加一个 Authorization
header 和 Bearer <access_token>
.
我有一个作业需要使用电子邮件和密码来验证用户并获取访问令牌。我同时拥有 api 密钥、秘密和基础 URL。我不需要为作业使用重定向 URL,也没有提供。我不确定要使用哪种方法或哪个库。我淹没在大量的信息中,这让我感到困惑。我需要指出正确的方向......欢迎任何形式的帮助。谢谢
我建议您使用 retrofit 库来做到这一点。
假设您的 URL 基础是 http://baseurl.com/api,您必须执行 GET 请求到 /login 并传递电子邮件和密码。我假设您的 API 将 return 一个用户对象作为 JSON.
Api.java
public interface Api {
@GET("/login")
public void login(@Query("email") String email, @Query("password"), Callback<User> callback);
}
需要执行 API 调用的位置:
Retrofit retrofit = new Retrofit.Builder()
.setEndpoint("http://baseurl.com")
.build();
Api api = retrofit.create(Api.class);
api.login(email, password, new Callback<User>() {
@Override
public void success(User user, Response response) {
// login logic
}
@Override
public void failure(RetrofitError error) {
Log.e("Retrofit", error.getMessage());
}
});
希望这个例子能帮到你。不要忘记阅读 retrofit documentation
根据您的评论,说明告诉您在规范中使用 Resource Owner Password Credentials Grant. You can see an example request。
POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=johndoe&password=A3ddj3w
唯一可能看起来很奇怪的事情(如果您从未遇到过)是 Authorization
header 值。继续阅读 Basic Authentication。基本上 czZCaGRSa3F0MzpnWDFmQmF0M2JW
是 username:password
的 base64 编码(实际上是 <client_id>:<client_secret>
)。
在不使用任何外部库(只是标准 Java 库)的情况下发出请求,您可能会有类似
String formData = "username=<uname>&password=<pass>&grant_type=password";
String header = "Basic " + Base64.encodeAsString("<client_id>:<client_secret>");
HttpURLConnection connection
= (HttpURLConnection) new URL(tokenUrl).openConnection();
connection.setDoOutput(true);
connection.addRequestProperty("Authorization", header);
connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", Integer.toString(formData.length()));
OutputStream out = connection.getOutputStream();
out.write(formData.getBytes(StandardCharsets.UTF_8));
InputStream in = connection.getInputStream();
AccessToken token = new ObjectMapper().readValue(in, AccessToken.class);
System.out.println(token);
out.close();
in.close();
我用的Base64
不是标准库class。 ObjectMapper
也不是标准库 class。我只是用它来解析对 AccessToken
class 的令牌响应。您可以使用任何您喜欢的解析器。 AccessToken
class 只包含所有可能的标记值
public class AccessToken {
public String access_token;
public String refresh_token;
public long expires_in;
public String token_type;
public String scope;
}
从那里开始,一旦你有了令牌,你想发出任何资源请求,你只需要添加一个 Authorization
header 和 Bearer <access_token>
.