簡體   English   中英

Android Google oAuth2“/oauth2/v4/token”請求顯示未找到異常文件

[英]Android Google oAuth2 “/oauth2/v4/token” request show exception file not found

應用程序需要谷歌刷新令牌,我嘗試調用基於谷歌 OAuth2 文檔的服務。

https://developers.google.com/identity/protocols/OAuth2InstalledApp

第 5 步:交換刷新和訪問令牌的授權代碼

服務拋出異常。

“java.io.FileNotFoundException: https : //www.googleapis.com/oauth2/v4/token

使用服務,如:

POST /oauth2/v4/token HTTP/1.1 主機:www.googleapis.com 內容類型:application/x-www-form-urlencoded

代碼=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7& client_id=your_client_id& client_secret=your_client_secret& redirect_uri= https://oauth2.example.com/code& grant_type=authorization_code

示例代碼:

class getTokenAsyncTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String JsonResponse = "";
            BufferedReader reader = null;
            HttpURLConnection urlConn;

            try {
                URL url = new URL("https://www.googleapis.com/oauth2/v4/token");
                String client_id = "******9-tt3qav2d2rp45sgjqp5helrsbvc22kdq.apps.googleusercontent.com";
                String client_secret = ""; //This value is not needed for clients registered as Android, iOS, or Chrome applications.
                String redirect_uri = "http://localhost";
                String grant_type = "authorization_code";
                String code = params[0]; // serverAuthCode code
                HashMap<String, String> params1 = new HashMap<String, String>();
                params1.put("client_id", client_id);
                params1.put("client_secret", client_secret);
                params1.put("grant_type", grant_type);
                params1.put("redirect_uri", redirect_uri);
                params1.put("code", code);  // your code received

                Set set = params1.entrySet();
                Iterator i = set.iterator();
                StringBuilder postData = new StringBuilder();
                for (Map.Entry<String, String> param : params1.entrySet()) {
                    if (postData.length() != 0) {
                        postData.append('&');
                    }
                    postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                    postData.append('=');
                    postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
                }
                byte[] postDataBytes = postData.toString().getBytes("UTF-8");

                HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
                conn.setDoOutput(true);
                conn.getOutputStream().write(postDataBytes);
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                StringBuilder builder = new StringBuilder();
                for (String line = null; (line = reader.readLine()) != null; ) {
                    builder.append(line).append("\n");
                }
                reader.close();
                conn.disconnect();
                System.out.println("token returned: " + builder.toString());

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

請建議我,如何在 android 應用程序中調用服務和接收刷新令牌 ID。

conn.setRequestMethod("GET");

應該是

conn.setRequestMethod("POST");

因為您正在發送發布請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM