Skip to content

Commit

Permalink
📝 修复 twitter 平台无法获取用户邮箱的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyd-c committed Jul 28, 2021
1 parent 4c8fdba commit 881a87e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/me/zhyd/oauth/config/AuthDefaultSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ public String accessToken() {

@Override
public String userInfo() {
return "https://api.twitter.com/1.1/users/show.json";
return "https://api.twitter.com/1.1/account/verify_credentials.json";
}
},

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/me/zhyd/oauth/request/AuthTwitterRequest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.zhyd.oauth.request;

import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.utils.HttpUtils;
import com.xkcoding.http.constants.Constants;
import com.xkcoding.http.support.HttpHeader;
import com.xkcoding.http.util.MapUtil;
Expand All @@ -11,6 +10,7 @@
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.utils.GlobalAuthUtils;
import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.utils.UrlBuilder;

import java.util.HashMap;
Expand Down Expand Up @@ -47,7 +47,7 @@ public AuthTwitterRequest(AuthConfig config, AuthStateCache authStateCache) {
*/
@Override
public String authorize(String state) {
AuthToken token = this.getRequestToken();
AuthToken token = this.getRequestToken();
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("oauth_token", token.getOauthToken())
.build();
Expand Down Expand Up @@ -119,17 +119,15 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
Map<String, String> queryParams = new HashMap<>(5);
queryParams.put("user_id", authToken.getUserId());
queryParams.put("screen_name", authToken.getScreenName());
queryParams.put("include_entities", Boolean.toString(true));
queryParams.put("include_email", Boolean.toString(true));

Map<String, String> oauthParams = buildOauthParams();
oauthParams.put("oauth_token", authToken.getOauthToken());

Map<String, String> params = new HashMap<>(oauthParams);
params.putAll(queryParams);
oauthParams.put("oauth_signature", generateTwitterSignature(params, "GET", source.userInfo(), config.getClientSecret(), authToken
.getOauthTokenSecret()));
oauthParams.put("oauth_signature", generateTwitterSignature(params, "GET", source.userInfo(), config.getClientSecret(), authToken.getOauthTokenSecret()));
String header = buildHeader(oauthParams);

HttpHeader httpHeader = new HttpHeader();
Expand All @@ -147,6 +145,7 @@ protected AuthUser getUserInfo(AuthToken authToken) {
.blog(userInfo.getString("url"))
.location(userInfo.getString("location"))
.avatar(userInfo.getString("profile_image_url"))
.email(userInfo.getString("email"))
.source(source.toString())
.token(authToken)
.build();
Expand All @@ -155,9 +154,8 @@ protected AuthUser getUserInfo(AuthToken authToken) {
@Override
protected String userInfoUrl(AuthToken authToken) {
return UrlBuilder.fromBaseUrl(source.userInfo())
.queryParam("user_id", authToken.getUserId())
.queryParam("screen_name", authToken.getScreenName())
.queryParam("include_entities", true)
.queryParam("include_email", true)
.build();
}

Expand Down

0 comments on commit 881a87e

Please sign in to comment.