2015-07-28 18 views
5

Tôi đang cố gắng triển khai Reddit oAuth2 (mọi ứng dụng sử dụng nội dung Reddit phải có số này implemented) trong ứng dụng 'không có người dùng' dựa trên Android và tôi đang làm theo hướng dẫn .Reddit oAuth 2 dành cho Android "không có người dùng" với Retrofit

  1. I registered an app và nhận số client_id tương ứng.
  2. Tôi đang theo dõi điều này cho API guidelinesthis for Retrofit để viết mã Android chính xác.

Do đó, tôi đã mã hóa hai phương pháp tiếp cận vấn đề và có vẻ như không hoạt động. Các cuộc gọi trong Fragment thích hợp là như nhau cho hai lựa chọn và nó đi như sau:

public void oAuth(){ 

    String bodyString = "grant_type=" + "https://oauth.reddit.com/grants/installed_client" 
         + "&device_id=" + UUID.randomUUID().toString(); 

    TypedInput requestBody = new TypedByteArray("application/x-www-form-urlencoded", bodyString.getBytes(Charset.forName("UTF-8"))); 

    RedditAPI.sRedditAuth().redditAuth(requestBody, new Callback<TokenResponse>() { 
     @Override 
     public void success(TokenResponse tokenResponse, Response response) { 
      Log.d("OATH_TAG", "oAuth() | YAY! :)"); 
     } 

     @Override 
     public void failure(RetrofitError error) { 
      Log.d("OATH_TAG", "oAuth() | NOOOOOoooooo.... :("); 
     } 
     }); 
    } 

OPTION 1:

  • giao diện Retrofit:

    public interface RedditAuthInterface { 
        @POST(Urlz.REDDIT_OATH2_PATH) 
        void redditAuth(@Body TypedInput body, Callback<TokenResponse> result); 
    
    } 
    
        //the adapter 
        public static RedditAuthInterface sRedditAuth() { 
        if (sRedditAuthInterface == null) { 
        RestAdapter restAdapter = new RestAdapter 
                .Builder() 
                .setClient(getAuthClient()) 
                .setEndpoint(Urlz.BASE_REDDIT_URL) 
                .build(); 
        sRedditAuthInterface = restAdapter.create(RedditAuthInterface.class); 
        } 
    
        return sRedditAuthInterface; 
    } 
    
    
    /* support methods */ 
    private static OkClient getAuthClient() { 
    
    final OkHttpClient okHttpClient = new OkHttpClient(); 
    okHttpClient.setReadTimeout(Static.READ_TIMEOUT, TimeUnit.SECONDS); 
    okHttpClient.setConnectTimeout(Static.CONNECT_TIMEOUT, TimeUnit.SECONDS); 
    /*okHttpClient.setAuthenticator(new Authenticator() { 
        @Override 
        public Request authenticate(Proxy proxy, Response response) throws IOException { 
         String credential = Credentials.basic(BldCnfg.REDDIT_CLIENT_ID, BldCnfg.REDDIT_PASS); 
         return response.request().newBuilder().header("Authorization", credential).build(); 
        } 
    
        @Override 
        public Request authenticateProxy(Proxy proxy, Response response) throws IOException { 
         return null; 
        } 
    });*/ 
    
    okHttpClient.networkInterceptors().add(OAUTH_INTERCEPTOR); 
    
        return new OkClient(okHttpClient); 
    } 
    
    private static final Interceptor OAUTH_INTERCEPTOR = new Interceptor() { 
    @Override 
    public Response intercept(Chain chain) throws IOException { 
        Response originalResponse = chain.proceed(chain.request()); 
        String credentials = BldCnfg.REDDIT_CLIENT_ID + ":" + BldCnfg.REDDIT_PASS; // REDDIT_PASS = "" as by API guides 
        String string = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); 
    
        originalResponse.header("Authorization", string); 
        originalResponse.header("Accept", "application/json"); 
        return originalResponse; 
        } 
    }; 
    
  • kết quả:

RetrofitError: 401 Unauthorized

OPTION 2:

  • giao diện Retrofit:

    public interface RedditAuthInterface { 
        @POST(Urlz.REDDIT_OATH2_PATH) 
        void redditAuth(@Body TypedInput body, Callback<TokenResponse> result); 
    
        } 
    
    
        //the adapter 
         public static RedditAuthInterface sRedditAuth() { 
         if (sRedditAuthInterface == null) { 
        RestAdapter restAdapter = new RestAdapter 
                .Builder() 
                .setClient(getConfuguredClient()) 
                .setRequestInterceptor(getRequestInerceptorPass()) 
                .setEndpoint(Urlz.BASE_REDDIT_URL) 
                .build(); 
        sRedditAuthInterface = restAdapter.create(RedditAuthInterface.class); 
    } 
    
    return sRedditAuthInterface; 
    } 
    
    
    /* support methods */ 
    
    public static RequestInterceptor getRequestInerceptorPass() { 
    RequestInterceptor rqInter = new RequestInterceptor() { 
        @Override 
        public void intercept(RequestFacade request) { 
    
         String credentials = BldCnfg.REDDIT_CLIENT_ID + ":" + BldCnfg.REDDIT_PASS; // REDDIT_PASS = "" as by API guides 
         String string = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); 
         request.addHeader("Authorization", string); 
         request.addHeader("Accept", "application/json"); 
         } 
        }; 
    
    return rqInter; 
    } 
    
    
    private static OkClient getConfuguredClient() { 
    
    final OkHttpClient okHttpClient = new OkHttpClient(); 
    okHttpClient.setReadTimeout(Static.READ_TIMEOUT, TimeUnit.SECONDS); 
    okHttpClient.setConnectTimeout(Static.CONNECT_TIMEOUT, TimeUnit.SECONDS); 
    return new OkClient(okHttpClient); 
    } 
    
  • kết quả:

Dường như tôi nhận được phản hồi trống (I chỉ nhận được "*" cho phạm vi). Câu trả lời thành công trông như thế này:

enter image description here

và tiêu đề như thế này:

enter image description here

Bạn có bất kỳ ý tưởng những gì tôi làm sai? Có ai đã làm điều này không?

The official Reddit github wiki thiếu các ví dụ của Android (mặc dù có hầu hết mọi ngôn ngữ khác).

Trả lời

0

Tôi đã trải qua cùng một vấn đề trước và thực hiện việc này library để trao OAuth2 trong Android. và thư viện là phần mở rộng cho Retrofit, giúp đơn giản hóa quy trình xác thực đối với nhà cung cấp OAuth 2.

+0

Bạn có sử dụng thư viện này để xác thực người dùng reddit không? Việc triển khai oauth của Reddit hơi khác thường nên tôi không chắc liệu thư viện có hỗ trợ nó hay không. –

+0

Thực ra, tôi không sử dụng nó trên Reddit. Đọc ReadMe trong Repo, Nếu nó không phục vụ mục đích của bạn cho tôi biết hoặc bạn có thể ngã ba dự án và những gì bạn cần – abozaid

0

Dựa trên hình ảnh của bạn với phản hồi "trống", cho thấy rằng bạn đã nhận được * trở lại dưới dạng phạm vi, tôi nghi ngờ rằng định nghĩa của bạn cho phản hồi mã thông báo truy cập đang sử dụng trường hợp lạc đà thay vì trường hợp rắn, vì vậy JSON không nhận được được nạp đúng vào đối tượng Java.

Các vấn đề liên quan