【问题标题】:How to pass a Bearer API Key in Retrofit2?如何在 Retrofit2 中传递承载 API 密钥?
【发布时间】:2019-04-11 16:29:06
【问题描述】:

两种可能性:要么我传递带有“授权:APIKEY”的标头,但它说我没有访问权限(代码 401),或者我传递“授权:承载 APIKEY”并引发异常。在查找 SO 之后,我找到了添加拦截器的解决方案,还添加了带有“Connection: close”的标题,但我仍然没有收到数据。

这是我的代码:

public class RetrofitYouSign {

    private static Retrofit sRetrofit;
    private static final String URL = "https://staging-api.yousign.com/";

    private static OkHttpClient sOkHttpClient = new OkHttpClient.Builder()
            .addInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Interceptor.Chain chain) throws IOException {
                            Request request = chain.request().newBuilder()
                                    .addHeader("Authorization", "Bearer MY-API-KEY")
                                    .addHeader("Content-Type", "application/json")
                                    .addHeader("Connection", "close")
                                    .build();
                            return chain.proceed(request);
                        }
                    }).build();

    public static Retrofit getRetrofit(){
        if (sRetrofit == null) {
            sRetrofit = new Retrofit.Builder()
                    .client(sOkHttpClient)
                    .baseUrl(URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return sRetrofit;
    }

    public interface GetYouSign {

        @GET("users/")
        Call<Yousign> getYouSignData();
    }

}```

And here is my stack if i enter with the name Bearer :

2019-04-11 19:25:12.894 1492-1492/com.jmjsolution.solarup I/System.out: [okhttp3.internal.http.StatusLine.parse(StatusLine.java:69), okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189), okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147), okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121), okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121), okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147), okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121), com.jmjsolution.solarpro.services.retrofitClient.RetrofitYouSign$1.intercept(RetrofitYouSign.java:31), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121), okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200), okhttp3.RealCall$AsyncCall.execute(RealCall.java:147), okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32), java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162), java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636), java.lang.Thread.run(Thread.java:764)]
2019-04-11 19:25:12.894 1492-1492/com.jmjsolution.solarup W/System.err: java.net.ProtocolException: Unexpected status line: ��
2019-04-11 19:25:12.894 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.StatusLine.parse(StatusLine.java:69)
2019-04-11 19:25:12.894 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
2019-04-11 19:25:12.894 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
2019-04-11 19:25:12.896 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-04-11 19:25:12.896 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
2019-04-11 19:25:12.897 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-04-11 19:25:12.897 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-04-11 19:25:12.897 1492-1492/com.jmjsolution.solarup W/System.err:     at com.jmjsolution.solarpro.services.retrofitClient.RetrofitYouSign$1.intercept(RetrofitYouSign.java:31)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err:     at java.lang.Thread.run(Thread.java:764)

Otherwise i receiver a code 401, if i don't put the word Bearer.

I tried the code with the interceptor (as you can see in my code) but nothing seems to work. Any solution would be really appreciated, thank you.

【问题讨论】:

  • 您必须优先保存它并在您的改装适配器中将它拿到这里并通过它。
  • 优先保存是什么意思?偏好管理器?为什么我拥有它并且我通过了它我只是没有出于隐私目的而写它!

标签: android authorization retrofit2 interceptor bearer-token


【解决方案1】:

试试这个 int APiInterface 类:-

@GET("users")
Call<Yousign> getYouSignData(@Header("Authorization") String token);

或者像这样在你的活动中:-

getYouSignData("Bearer your api key")

【讨论】:

  • 从 @GET("users") 中删除 /
  • 我已经傻了好几个小时了!非常感谢你 !只是关于用户/与用户!
【解决方案2】:

您可以简单地将您 (bearer + auth token) 作为 Authorization 参数传递,如下所示:

@GET("api/GetProfile)
Call<UserProfile> getProfile(@Header("Authorization") String authHeader); 

然后这样调用

 Call<UserProfile> calltargetResponce = client.getProfile("Bearer "+token);

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2013-03-21
    • 2012-03-24
    • 2018-10-10
    • 2012-01-23
    • 2018-08-12
    • 1970-01-01
    • 2016-10-10
    • 2016-05-23
    相关资源
    最近更新 更多