您可以驗證access_token
是否有效。
您需要發送GET
請求到api終點:https://www.googleapis.com/oauth2/v1/tokeninfo
請求您的access_token。
,您可以嘗試這樣的:
String connection = new ConnectionService().connectionGoogle("https://www.googleapis.com/oauth2/v1/tokeninfo", "access_token=" + "YOUR_EXISTING_ACCESS_TOKEN");
System.out.println(connection);
在上面的代碼中使用的方法是:
public static String connectionGoogle(String url, String parameter) throws MalformedURLException, ProtocolException, IOException {
URL url1 = new URL(url);
HttpURLConnection request1 = (HttpURLConnection) url1.openConnection();
request1.setRequestMethod("GET");
request1.setDoOutput(true);
request1.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter wr = new OutputStreamWriter(request1.getOutputStream());
wr.write(parameter);
wr.flush();
request1.connect();
String responseBody = convertStreamToString(request1.getInputStream());
wr.close();
return responseBody;
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
} finally {
try {
is.close();
} catch (IOException e) {
}
}
return sb.toString();
}
我不知道有關的問題。由於你的應用程序直接與谷歌服務器進行通信,因此你的個人服務器在哪裏出現?你可以檢查你的android應用程序本身的標記有效性。你能詳細說明嗎? – drulabs
我已經從客戶端應用程序(Android)中獲取g +的令牌,我將該訪問令牌發送到我的服務器,在那個服務器中,我想重新驗證從Google發起的訪問令牌或者不需要獲取用戶信息...使用該訪問令牌....感謝回覆 –
嘗試任何操作。像搜索或上傳或任何東西,如果令牌是有效的,你會得到答覆,否則你會收到一個「未經授權」的迴應。通常,令牌每過一小時或用戶明確更改Google密碼時過期。如果您已正確完成身份驗證,則必須使用訪問令牌收到刷新令牌。使用此刷新令牌獲取新的訪問令牌。 – drulabs