来自google API文档url的最后一段"安全地将serverAuthCode字符串传递到您的服务器,这将交换访问令牌和刷新令牌的代码"。我不知道该怎么处理serverAuthCode
。用serverAuthCode
得到accessToken
和refreshToken
的代码样本在哪里?
com给出的解。Iavian是完美的,
在我的情况下,我得到serverAuthCode从https://github.com/react-native-google-signin/google-signin,
在后台,然后你可以做下面的代码
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
public static final JsonFactory JSON_FACTORY = new JacksonFactory();
GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, Your_OAUTH_CLIENTID, Your_OAUTH_CLIENTSECRET, Serverauthcode_from_react_native, "").execute();
注意:serverAuthCode只能使用一次,从您获得的响应中您应该存储refreshToken,并从下一次使用refreshToken获取您的AccessToken
如何做
GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(httpTransport, jsonFactory, Main.GMAIL_OAUTH_CLIENTID, Main.GMAIL_OAUTH_CLIENTSECRET, X.getServerauthcode(), "").execute();
X.setServeraccesstoken(response.getAccessToken());
X.setServerrefreshtoken(response.getRefreshToken());