gpt4 book ai didi

android - Reddit oAuth 2 for Android "userless"app with Retrofit

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:24:32 25 4
gpt4 key购买 nike

我正在尝试在基于 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:

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

RetrofitError: 401 Unauthorized

选项 2:

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

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

enter image description here

和这样的标题:

enter image description here

你知道我做错了什么吗?有没有人这样做过?

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

最佳答案

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

关于android - Reddit oAuth 2 for Android "userless"app with Retrofit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31680286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com