我尝试了Google's documentation的授权过程,但没有收到Google的回复。我尝试了OkHttp,但它不起作用。使用Google登录登录后,应用程序崩溃。 我已经有crendatial.json,client_secret.json和来自Google的身份验证令牌 由于我需要将客户机密信息发送给Google,因此我想知道是否以正确的格式发送了该信息?
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
// create Google sign-in account upon Google sign-in call
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
String idToken = account.getIdToken();
String authCode = account.getServerAuthCode();
// create user, save to database
AddUserToDatabase(account);
// post request to Google
InputStream isClientSecret = getResources().openRawResource(R.raw.client_secret);
InputStreamReader isrClientSecret = new InputStreamReader(isClientSecret, Charset.forName("UTF-8"));
Log.w("Before Response: ", "Lets GO!!");
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(
JacksonFactory.getDefaultInstance(), isrClientSecret);
OkHttpClient client = new OkHttpClient();
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("client_id", clientSecrets.getDetails().getClientId())
.addFormDataPart("client_secret",clientSecrets.getDetails().getClientSecret())
.addFormDataPart("AuthCOde",authCode)
.addFormDataPart("RedirectURL","")
.build();
Request request = new Request.Builder()
.url("https://www.googleapis.com/oauth2/v4/token")
.post(body)
.build();
try {
Response response = client.newCall(request).execute();
Log.w("Response: ", response.body().toString());
} catch (IOException e) {
e.printStackTrace();
}
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
} catch (FileNotFoundException e) {
Log.w("File not found:::", "FNF");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这是Google文档中的实现。
GoogleTokenResponse tokenResponse =
new GoogleAuthorizationCodeTokenRequest(
new NetHttpTransport(),
JacksonFactory.getDefaultInstance(),
"https://www.googleapis.com/oauth2/v4/token",
clientSecrets.getDetails().getClientId(),
clientSecrets.getDetails().getClientSecret(),
authCode,
"").execute();
// redirect uri = "" because we don't have a back-end server.