【问题标题】:Reddit oAuth 2 for Android "userless" app with RetrofitReddit oAuth 2 for Android“无用户”应用程序与改造
【发布时间】:2015-10-19 05:51:06
【问题描述】:

我正在尝试在基于 Android 的“无用户”应用程序中实施 Reddit oAuth2(每个使用 Reddit 内容的应用程序都必须有这个 implemented),并且我正在遵循指南。

  1. registered an app 并得到相应的client_id
  2. 我正在关注 API guidelinesthis for Retrofit 以便正确编写 Android 代码。

因此,我编写了两种解决问题的方法,但似乎都不起作用。对于这两个选项,相应 Fragment 中的调用是相同的,如下所示:

    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.... :(");
        }
      });
    }

选项 1:

  • 改造界面:

      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;
         }
     };
    
  • 结果:

RetrofitError: 401 Unauthorized

选项 2:

  • 改造界面:

       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);
    }
    
  • 结果:

似乎我得到了空的响应(我只得到“*”作为范围)。成功响应如下所示:

和这样的标题:

你有什么想法我做错了什么吗? 有人做过吗?

The official Reddit github wiki 缺少 Android 示例(但几乎所有其他语言都有)。

【问题讨论】:

    标签: android oauth oauth-2.0 retrofit reddit


    【解决方案1】:

    我之前也遇到过同样的问题,并将这个library 用于在 Android 中处理 OAuth2。该库是 Retrofit 的扩展,它简化了针对 OAuth 2 提供者进行身份验证的过程。

    【讨论】:

    • 您是否使用此库对 reddit 用户进行身份验证? Reddit 的 oauth 实现有点不寻常,所以我不确定库是否会支持它。
    • 其实我在Reddit上没用过。在 Repo 中阅读自述文件,如果它没有达到您的目的,请告诉我,或者您可以分叉项目以及您需要什么
    【解决方案2】:

    根据您带有“空”响应的图像,显​​示您将* 作为范围返回,我怀疑您对访问令牌响应的定义是使用驼峰式大小写而不是蛇形大小写,因此 JSON 不是正确加载到 Java 对象中。

    【讨论】:

      猜你喜欢
      • 2013-12-01
      • 2014-06-01
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 2019-01-17
      • 2012-02-25
      • 2015-03-11
      • 2015-11-17
      相关资源
      最近更新 更多