改造记录错误

时间:2018-06-16 08:22:06

标签: android

我使用OAuth2的密码授予流程对API EndPoint进行身份验证。我正在使用的库是Retrofit。

我收到以下错误:

D/OkHttp: {"error":"invalid_clientId","error_description":"ClientId should be sent."}

我想确切地看到我发送的API调用,所以我可以检查它是否有错误..我在网上搜索过,发现你需要添加一个LoggingInterceptor,我正在做。仍未获得完整的API调用..

在我的日志中,我得到了我发送的标题的跟踪,以及URL,但不是正文参数..有没有办法在日志记录中获取正文参数?

我希望看到每个参数都带有发送值..

可以在以下网址找到包含临时凭证的代码:

https://github.com/NVwingh84/ATTAPITest

我正在使用的代码是:

public class MainActivity extends AppCompatActivity {

private String grant_type = "password";
private String username = "xxxxx";
private String password = "xxxxx";
private String clientId = "xxxxx";
private String client_secret = "xxxxx";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AccessTokenRequest mytokenrequest =  new AccessTokenRequest(grant_type,username,password,clientId,client_secret);
    sendNetworkRequest(mytokenrequest);
}

public void sendNetworkRequest(AccessTokenRequest accessTokenRequest){

    //create okhttpclientbuilder and set up logging for full logging level "Body"
    OkHttpClient.Builder okhttpclientbuilder = new OkHttpClient.Builder();
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    //App will only log if you are in development mode
    if (BuildConfig.DEBUG){
        okhttpclientbuilder.addInterceptor(loggingInterceptor);
    }

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("https://api.allthingstalk.io")
            .addConverterFactory(GsonConverterFactory.create())
            .client(okhttpclientbuilder.build());

    Retrofit retrofit = builder.build();

    AccessTokenClient client = retrofit.create(AccessTokenClient.class);
    Call<AccessTokenRequest> call = client.getAccessToken();
    call.enqueue(new Callback() {
        @Override
        public void onResponse(Call call, Response response) {
            Toast.makeText(MainActivity.this, "EndPoint Response", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call call, Throwable t) {
            Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
        }
    });
};
}

1 个答案:

答案 0 :(得分:0)

试试这段代码。

将以下依赖项添加到app level gradle文件中..

    implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'

并为改装对象定义创建单独的类,并且像下面的代码一样容易访问..

public class ApiClient {
private final static String BASE_URL = "https://dog.ceo/api/breed/";

public static ApiClient apiClient;
private Retrofit retrofit = null;

public static ApiClient getInstance() {
    if (apiClient == null) {
        apiClient = new ApiClient();
    }
    return apiClient;
}

//private static Retrofit storeRetrofit = null;

public Retrofit getClient() {
    return getClient(null);
}


private Retrofit getClient(final Context context) {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            return chain.proceed(request);
        }
    });

    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    return retrofit;
}
}