Twitter login Callback URL not approved for this client application error record

Because the app I made was for the overseas version, I suddenly said that Twitter login failed in the past two days. I checked various reasons and solutions, and finally solved it, and provided two solutions.

The Twitter boss updated the authorization and authentication mechanism, and the application backend was not updated, which caused a large area of ​​login problems, which was very collapsed!

Problem Description

If the Twitter client is installed on the mobile phone, it has no effect. If it is not installed, it will prompt login failure and cannot log in.

Without further ado, connect to the phone to read the log:

06-15 16:22:25.734 1015-1015/com.example.testlogin E/Twitter: Invalid json: <?xml version="1.0" encoding="UTF-8"?><errors><error code="415">Callback URL not approved for this client application. Approved callback URLs can be adjusted in your application settings</error></errors>
                                                              com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
                                                                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
                                                                  at com.twitter.sdk.android.core.models.SafeListAdapter$1.read(SafeListAdapter.java:45)
                                                                  at com.twitter.sdk.android.core.models.SafeMapAdapter$1.read(SafeMapAdapter.java:45)
                                                                  at com.google.gson.Gson.fromJson(Gson.java:887)
                                                                  at com.google.gson.Gson.fromJson(Gson.java:852)
                                                                  at com.google.gson.Gson.fromJson(Gson.java:801)
                                                                  at com.google.gson.Gson.fromJson(Gson.java:773)
                                                                  at com.twitter.sdk.android.core.TwitterApiException.parseApiError(TwitterApiException.java:110)
                                                                  at com.twitter.sdk.android.core.TwitterApiException.readApiError(TwitterApiException.java:95)
                                                                  at com.twitter.sdk.android.core.TwitterApiException.<init>(TwitterApiException.java:43)
                                                                  at com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42)
                                                                  at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
                                                               Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
                                                                  at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
                                                                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
                                                                  at com.twitter.sdk.android.core.models.SafeListAdapter$1.read(SafeListAdapter.java:45) 
                                                                  at com.twitter.sdk.android.core.models.SafeMapAdapter$1.read(SafeMapAdapter.java:45) 
                                                                  at com.google.gson.Gson.fromJson(Gson.java:887) 
                                                                  at com.google.gson.Gson.fromJson(Gson.java:852) 
                                                                  at com.google.gson.Gson.fromJson(Gson.java:801) 
                                                                  at com.google.gson.Gson.fromJson(Gson.java:773) 
                                                                  at com.twitter.sdk.android.core.TwitterApiException.parseApiError(TwitterApiException.java:110) 
                                                                  at com.twitter.sdk.android.core.TwitterApiException.readApiError(TwitterApiException.java:95) 
                                                                  at com.twitter.sdk.android.core.TwitterApiException.<init>(TwitterApiException.java:43) 
                                                                  at com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42) 
                                                                  at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68) 
                                                                  at android.os.Handler.handleCallback(Handler.java:751) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                  at android.os.Looper.loop(Looper.java:154) 
                                                                  at android.app.ActivityThread.main(ActivityThread.java:6776) 
                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518) 
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 

There is a string below, but this is the main reason, so I won’t read it below. Under the analysis, the json parsing failed. Normally, it was successful. Now a string of xml code is returned, which of course failed. Then the problem is the wrong description in the xml.
Callback URL not approved for this client application. Approved callback URLs can be adjusted in your application settings
. This is the problem. There is a problem with the Callback URL setting in the Twitter application background, but this thing hasn't been moved at all. Why does it suddenly stop working?

Find a solution

Check if Twitter is doing anything? Sure enough, I finally found that Twitter issued an announcement in May, reminding users who log in using Twitter. If they need to continue to log in using Twitter, they need to add the Callback URL to the whitelist in the background of the app. OK, the solution is there, in the end What to fill in? To tell the truth, Twitter officials have not made it clear, and there have been questions everywhere these days. . . . .

Write picture description here

During the period, various blogs, communities, and official websites described the Callback URL and just fill in an address that can be accessed. I also wrote a wave according to the official integrated android-kit, but it still doesn't work, the same error. collapse.

Solution 1

Finally found one issue in a Callback URL, is said to be url Twitter official use it to fill in our application background whitelist the ok
for it is: twittersdk: //
After filling in like this:
Write picture description here

In this way, the client does not need to move anything and continue to use it normally.

Solution 2

Another solution, this change is big, and the client code needs to be updated. Because of the urgency, I also learned about the wave and wrote a demo to receive it (then I found the first solution, and I returned in minutes Return code).

This solution is to use Twitter's Auth authorization api to load the page directly, yes, load it yourself.
There is an api interface for authentication in the api of the Twitter developer’s official website. The
Write picture description here
parameter is an oauth_token that must be passed and
finally assembled is such a url and page.
Write picture description here

So, what we need to load is this page, just write a webView to load this address!
Finally, how to get the token, please directly upload the code:

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey(KEY);
configurationBuilder.setOAuthConsumerSecret(SECRET);
Configuration configuration = configurationBuilder.build();
mTwitter = new TwitterFactory(configuration).getInstance();
   new Thread(new Runnable() {
      @Override
      public void run() {
        try {
            requestToken = mTwitter.getOAuthRequestToken();
            runOnUiThread(new Runnable() {
               @Override
               public void run() {
                  webView.loadUrl(requestToken.getAuthenticationURL());
                  }
              });
           }catch (twitter4j.TwitterException e) {
              e.printStackTrace();
         }
      }
}).start();

In this way, you can also enter the authorization page, and then listen in WebviewClient. When you enter the authorization page after logging in, you can provide another method to obtain user information through Twitter.

accessToken = mTwitter.getOAuthAccessToken(requestToken, strVerifier);

The requestToken is the token and strVerifier that we obtained before. We get it from the url. There is a parameter in the link to the authorization webpage called oauth_verifier . We get this parameter through uri.getQueryParameter("oauth_verifier").

Note that this is a network operation and time-consuming.

Finally, the information in AccessToken is the information of twitter users.
Write picture description here

Stepping on the pit record, I hope to help everyone.

Guess you like

Origin blog.csdn.net/lizebin_bin/article/details/80707733