2017-04-27 113 views
0

我沒有在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嘗試都。

我試圖

  1. 與相同參數郵差相同的請求嘗試,它是確定。
  2. 嘗試沒有REDIRECT_URI
  3. 使用客戶端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) 
+0

Lol,404.你的網址有誤。不是你的代幣。如果是你的令牌,應該返回401. –

+0

沒有網址是正確的。我通過POSTMAN嘗試了同樣的方法,並且使用了DefaultHttpClient,它可以工作。 –

回答

0

謝謝你們的答案。我趕上我的錯誤。爲了使改造對象單身我正在檢查null,如果它不是空,我要返回舊的改造object.so我的基地URL不會改變它堅持到第一次設置基地址。 enter image description here

0

您是否嘗試過檢查你的改造要求的日誌設置基本身份驗證試過嗎?例如,可能是標題,請求參數甚至全服務URL可能不同於POSTMAN請求。

檢查下面的答案啓用日誌記錄在您的改造請求,並與郵差比較請求: https://stackoverflow.com/a/33256827/4862126

我還發現,當它可能發生的情況: 404:找不到

  1. 當請求的資源(使用提供的ID)從未存在
  2. 訪問用戶無法訪問的API。

響應:

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "notFound", 
    "message": "Not Found" 
    } 
    ], 
    "code": 404, 
    "message": "Not Found" 
} 
} 
+0

已啓用日誌看到更新問題日誌 –

0

您的基本網址需要與/結束,和您的服務入口點必須@POST("o/oauth2/token")

+0

嘗試但沒有運氣相同的錯誤。 –

相關問題