我没有在android客户端中使用retrofit2获取令牌。 我想从谷歌OAuth2服务器获取访问令牌。我从我以前的getAuthCode API获取授权码,这是成功的,我检查使用POSTMAN获取访问令牌。我得到未找到(404)错误。未在Android客户端中使用retrofit2获取令牌
D/OkHttp: <-- 404 https://www.googleapis.com/o/oauth2/token (95ms)
这里是我的okHttpClient代码
public class HttpClient {
private static OkHttpClient.Builder builder;
public static synchronized OkHttpClient getOkHttpClient(){
try {
TrustManager[] trustManagers = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
};
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null,trustManagers,new SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder = new OkHttpClient().newBuilder()
.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustManagers[0])
.addInterceptor(httpLoggingInterceptor)
.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return builder.build();
}
}
这里是改造的客户端代码
public class RetrofitClient {
private static Retrofit.Builder retrofit = null;
public static Retrofit getRetrofitClient(String baseUrl){
if(retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(HttpClient.getOkHttpClient())
.addConverterFactory(GsonConverterFactory.create());
}
return retrofit.build();
}
}
这里的接口
@POST("/o/oauth2/token")
@FormUrlEncoded
Call<AccessToken> getAccessToken(@Field("client_id") String clientId,
@Field("redirect_uri") String redirct,
@Field("client_secret") String secret,
@Field("grant_type") String grantType,
@Field("code") String code
);
这里是调用函数
AuthService authService = RetrofitClient.getRetrofitClient("https://accounts.google.com").create(AuthService.class);
authService.getAccessToken(authCode.getOauthClientId(),redirect,clientPassword,grantType,authCode).enqueue(new Callback<AccessToken>() {
@Override
public void onResponse(Call<AccessToken> call, Response<AccessToken> response) {
Log.d("AccessToken", "............." + response.isSuccessful());
Log.d("AccessToken",response.message());
if (response.code() == 200) {
Log.d("Test", ".............Access Token" + response.body().getAccessToken());
}
}
@Override
public void onFailure(Call<AccessToken> call, Throwable t) {
Log.d("Test", ".............Failed " + t.getMessage());
}
});
在上面的代码中,我使用的client_id和client_secret从控制台,grant_type = authorization_code,AUTH_CODE这是我从先前的API和grant_type =瓮得到:IETF:WG:OAuth的:2.0:OOB或仅OOB尝试都。
我试图
- 与相同参数邮差相同的请求尝试,它是确定。
- 尝试没有REDIRECT_URI
- 使用客户端ID和秘密
登录
D/OkHttp: --> POST https://www.googleapis.com/o/oauth2/token http/1.1
D/OkHttp: Content-Type: application/x-www-form-urlencoded
D/OkHttp: Content-Length: 221
D/OkHttp: client_id=7********-***********.apps.googleusercontent.com&redirect_uri=oob&client_secret=1-****************&grant_type=authorization_code&code=4%2Fohfz64qAyQY3K3HZc7zBplCCl4uje28RoP5fnFZwIDw
D/OkHttp: --> END POST (221-byte body)
D/OkHttp: <-- 404 https://www.googleapis.com/o/oauth2/token (79ms)
D/OkHttp: cache-control: no-cache, no-store, max-age=0, must-revalidate
D/OkHttp: pragma: no-cache
D/OkHttp: expires: Mon, 01 Jan 1990 00:00:00 GMT
D/OkHttp: date: Thu, 27 Apr 2017 08:38:54 GMT
D/OkHttp: vary: Origin
D/OkHttp: vary: X-Origin
D/OkHttp: content-type: text/html; charset=UTF-8
D/OkHttp: x-content-type-options: nosniff
D/OkHttp: x-frame-options: SAMEORIGIN
D/OkHttp: x-xss-protection: 1; mode=block
D/OkHttp: server: GSE
D/OkHttp: alt-svc: quic=":443"; ma=2592000; v="37,36,35"
D/OkHttp: Not Found
D/OkHttp: <-- END HTTP (9-byte body)
Lol,404.你的网址有误。不是你的代币。如果是你的令牌,应该返回401. –
没有网址是正确的。我通过POSTMAN尝试了同样的方法,并且使用了DefaultHttpClient,它可以工作。 –