1

I am using sign in with Slack API.

To obtain an access token, there is a HTTP GET request:

This is authorize url, I use this for login and implemented via WebView:

I am using WebView for login integration.

My classes are:

public class LoginApiClient {
 private static LoginService loginService;

 public static LoginService getLoginService(){
    if (loginService != null){
        Retrofit retrofitClient = new Retrofit.Builder()
                .baseUrl("https://slack.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        loginService = retrofitClient.create(LoginService.class);
    }
    return loginService;
 }
}

public interface LoginService {
    @FormUrlEncoded
    @GET("api/oauth.access")
    Call<LoginResponse> makeUserLogin(@FieldMap Map<String, String > parameter);
}

public class LoginResponse {
    .....
    @SerializedName("access_token")
    @Expose
    private String accessToken;
    .....
    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

}

In my activity class, inside shouldOverrideUrlLoading method:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("http://localhost:8000/")) {

            UrlQuerySanitizer urlQuerySanitizer = new UrlQuerySanitizer(url);
            codeParams = urlQuerySanitizer.getValue("code");
            Log.e(TAG, "shouldOverrideUrlLoading: " + codeParams);
            retrieveToken();
            return true;
        } else {
            view.loadUrl(url);
            return true;
        }
    }

private void retrieveToken() {
    LoginService loginService = LoginApiClient.getLoginService();
    Map<String, String> urlParameter = new HashMap<>();
    urlParameter.put("client_id", StringConstant.CLIENT_ID);
    urlParameter.put("client_secret", StringConstant.CLIENT_SECRET);
    urlParameter.put("code", codeParams);
    Call<LoginResponse> call = loginService.makeUserLogin(urlParameter);
    call.enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
            //navigate to next activity after saving token
        }

        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) {

        }
    });
}

Now, it is throwing the nullpointer error and I think the request url not working, so I get this.

java.lang.NullPointerException: Attempt to invoke interface method 'retrofit2.Call com.loginApi.LoginService.makeUserLogin(java.util.Map)' on a null object reference

How can I know that, the HTTP GET request url is hitting ? How can I do to build the url and after login it get hit and I get token ?

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
  • Can you remove this check if (loginService != null) and try once ? Let the static method return new instances. Probably it may get cleared in application run that is why you are getting null point exception – Sreehari Mar 06 '17 at 07:17
  • Your information will be helpful for me if you clarify what can i remove ? And, `(loginService != null)` is always `true`. – Satan Pandeya Mar 06 '17 at 08:40
  • In your above code, I can see only one line mentioning the same . Wondering why couldn't you detect in your code itself ? As I said always return new instance than checking that condition and try – Sreehari Mar 06 '17 at 09:04
  • Okay, thank you a lot there is the problem. I also wonder with me I can't see in time. – Satan Pandeya Mar 06 '17 at 09:45
  • Is that issue resolved ? – Sreehari Mar 06 '17 at 11:17
  • Yes, now the url works, i retrieve the token, I wonder that was the issue. – Satan Pandeya Mar 06 '17 at 11:41

2 Answers2

0

You can use https://slack.com/ for base url as common and in apiinterface create all method with dynamic url like..

@GET("api/oauth.access")
Call<JsonElement> oauth.access(@QueryMap HashMap<String, String> body);

@GET("oauth/authorize")
Call<JsonElement> oauth.access(@QueryMap HashMap<String, String> body);
Chirag.T
  • 746
  • 1
  • 6
  • 18
  • Of course, I am doing similar that. But the problem is, I don't know the dynamic url is getting all three parameter or not. So, i face problem to access the login token. – Satan Pandeya Mar 06 '17 at 06:24
  • yes definitely it took all three parameter just pass with hashmap it take your parameter with key and value and use with get method – Chirag.T Mar 06 '17 at 06:25
  • Does the url: https://slack.com/api/oauth.access?client_id=something&client_secret=something&code=something get request? – Satan Pandeya Mar 06 '17 at 06:30
  • slack.com/api/ is base url and after `oauth.access` and another is get is right..? – Chirag.T Mar 06 '17 at 06:34
0

Possibly due to the following statement where the loginService object is retrieved.

Remove this check

if (loginService != null)

and see if that resolves. Probably the application would have cleared the variable due to memory allocation/de-allocation

Sreehari
  • 5,621
  • 2
  • 25
  • 59