如何在Retrofit2中传递Bearer API密钥?

问题描述 投票:0回答:2

两种可能性:要么通过带有“授权: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
2个回答
3
投票

试试这个int APiInterface类: -

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

或者在你的活动中:

getYouSignData("Bearer your api key")

0
投票

你可以直接传递给你(bearer + auth token)作为授权参数,如下所示:

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

然后像这样打电话

 Call<UserProfile> calltargetResponce = client.getProfile("Bearer "+token);
© www.soinside.com 2019 - 2024. All rights reserved.