404 oAuth令牌API

时间:2018-07-11 06:36:02

标签: android oauth-2.0

Ghost错误,请在标记为重复之前阅读。 我面临一个非常奇怪的问题,

我有互联网连接接收器

private void connectionChange(Context foContext, Intent foIntent) {
    boolean lbIsNetworkAvaibale = false;

    ConnectivityManager cm = (ConnectivityManager) foContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo currentNetworkInfo = cm.getActiveNetworkInfo();
    if (currentNetworkInfo != null)
        if (currentNetworkInfo.isAvailable())
            lbIsNetworkAvaibale = currentNetworkInfo.isConnected();

    Log.i(TAG, "Network Available is: " + lbIsNetworkAvaibale);
    if (!lbIsNetworkAvaibale) {
        NetworkInfo otherNetworkInfo = (NetworkInfo) foIntent
                .getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
        if (otherNetworkInfo != null)
            if (otherNetworkInfo.isAvailable())
                lbIsNetworkAvaibale = otherNetworkInfo.isConnected();
    }

    Log.i(TAG, "Network Available New is: " + lbIsNetworkAvaibale);
    if (lbIsNetworkAvaibale) {
        // Calling the method and from that method call token api using volley will give you code below.
    } else {
        // stop all service and alarms  
   }
}

齐射代码

public static int callVolleyPostRequest(final boolean fbIsRefreshToken, final Context foContext, final RefreshTokenResponses loRefreshTokenResponses) {

    String lsRequestURL = "http://my.domain/auth/token";

    final SharedPreferenceManager loSharedPreferenceManager = new SharedPreferenceManager(foContext);
    StringRequest jsonObjReq = new StringRequest(com.android.volley.Request.Method.POST, lsRequestURL,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    Log.i(TAG, "Volley Response : " + response);

                    if (!TextUtils.isEmpty(response)) {
                        try {

                            JSONObject loJsonResponse = new JSONObject(response);
                            if ((loJsonResponse.length() <= 0)) {
                                loRefreshTokenResponses.onResponse(400);
                                return;
                            } else {
                                //storing token in preference
                                loRefreshTokenResponses.onResponse(200);
                            }
                        } catch (Exception e) {
                            Log.logException(TAG, e);
                        }
                    }
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            if (error == null || error.networkResponse == null) {
                Log.i(TAG, "Volley Error null");
                return;
            }

            String body = "";
            //get status code here
            int liStatusCode = error.networkResponse.statusCode;
            loRefreshTokenResponses.onResponse(liStatusCode);

            //get response body and parse with appropriate encoding
            try {
                body = new String(error.networkResponse.data, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // exception
            }
            Log.i(TAG, "Error: " + error.getMessage());
            Log.i(TAG, "Error body : " + body);

            Log.i(TAG, "Volley Error code : " + liStatusCode);

        }
    }) {

        @Override
        public String getUrl() {
            String lsUrl = super.getUrl();
            Log.i(TAG, "Volley token request URL : " + lsUrl);
            return lsUrl;
        }



        @Override
        public byte[] getBody() {
            try {
                Log.i(TAG, "fbIsRefreshToken  : " + fbIsRefreshToken);
                HashMap<String, String> loStringRequest = new HashMap<String, String>();
                try {

                    if (!fbIsRefreshToken) {

                      loStringRequest.put(Constants.TOKEN.USERNAME.toString(), TextUtils.isEmpty(loSharedPreferenceManager.getUserName()) ? "" : loSharedPreferenceManager.getUserName());
                            loStringRequest.put(Constants.TOKEN.PASSWORD.toString(), TextUtils.isEmpty(loSharedPreferenceManager.getPassword()) ? "" : loSharedPreferenceManager.getPassword());
                            loStringRequest.put(Constants.TOKEN.GRANT_TYPE.toString(), Constants.TOKEN.PASSWORD.toString());
                            int liLoginType = loSharedPreferenceManager.getLoginType() == 0 ? 1 : loSharedPreferenceManager.getLoginType();
                            loStringRequest.put(Constants.TOKEN.LOGINTYPE.toString(), "" + liLoginType);

                    } else {
                        loStringRequest.put(Constants.TOKEN.REFRESH_TOKEN.toString(), TextUtils.isEmpty(loSharedPreferenceManager.getRefreshToken()) ? "" : loSharedPreferenceManager.getRefreshToken());
                        loStringRequest.put(Constants.TOKEN.GRANT_TYPE.toString(), Constants.TOKEN.REFRESH_TOKEN.toString());
                    }
                } catch (Exception e) {
                    Log.logException(TAG, e);

                    Log.i(TAG, "Exception  getToken ");
                }
                Log.i(TAG, "Volley token build param : " + getPostData(loStringRequest));
                return getPostData(loStringRequest).getBytes();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return null;
        }
    };

    // Adding request to request queue
    AppGlobal.getInstance().addToRequestQueue(jsonObjReq, "Token");
    return 0;
}

错误日志

<!DOCTYPE html>
    <html lang=en>
      <meta charset=utf-8>
         <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
           <title>Error 404 (Not Found)!!1</title>
         <style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
 </style>
   <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
   <p><b>404.</b> <ins>That’s an error.</ins>
   <p>The requested URL <code>/auth/token</code> was not found on this server.  <ins>That’s all we know.</ins>

HTML说

enter image description here

现在奇怪的是,我在5到10个小时后以某种方式测试应用程序一切正常(即使我以100次连接和断开Internet在2g网中进行测试),互联网连接也以某种方式改变了2 3次,然后尝试启动令牌请求两次调用均出现404错误(使用刷新令牌获取访问令牌,并使用unm pass获取新令牌)。这将继续,并且当我强制从设置中停止该应用程序并再次打开该应用程序时,它将开始正常工作:(。

我怀疑问题出在后端,但是正如我所说,当我强制重启应用程序时,它将起作用。和后端代码运行良好。

仅供参考:-我首先使用了Asynctask,但遇到了错误,因此请替换为volley,身份验证机制为oAuth 2.0,而不是Google,并且该应用程序继续在后台运行。

请避免语法:)和帮助。卡了好几天。

0 个答案:

没有答案