java 为什么LinkedIn API返回404当我尝试获取电子邮件地址

fykwrbwg  于 2023-03-16  发布在  Java
关注(0)|答案(2)|浏览(164)

我尝试从LinkedIn API获取登录用户的电子邮件地址,所以我在以下位置获取请求:https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~)),授权头为Bearer access_token。但我得到的响应为404

{"serviceErrorCode":0,"message":"Resource null does not exist","status":404}

配置文件数据为/v2/me的端点工作正常。我做错了什么?
示例代码尚未重构,仅用于获取响应和请求日志。不要受变量名称的影响:

webClient = WebClient.builder()
            .baseUrl("https://api.linkedin.com")
            .clientConnector(new ReactorClientHttpConnector(HttpClient.create().wiretap(this.getClass().getCanonicalName(), LogLevel.INFO, AdvancedByteBufFormat.TEXTUAL)))
            .build();
        webClient.get()
            .uri(uriBuilder -> uriBuilder.path("/v2/me").build())
            .header("Authorization", "Bearer " + facebook.getAccessToken())
            .retrieve()
            .bodyToMono(GoogleProfile.class)
            .log()
            .block();
        webClient.get()
            .uri(uriBuilder -> uriBuilder.path("v2/emailAddress?q=members&projection=(elements*(handle~))").build())
            .header("Authorization", "Bearer " + facebook.getAccessToken())
            .retrieve()
            .bodyToMono(GoogleProfile.class)
            .log()
            .block();

我的客户代码:

const redirectUri = 'http://localhost:8082/login/linkedin/callback';
    const scope = 'r_liteprofile r_emailaddress';
    const clientId = 'client_code';
    const responseType = 'code';

    const linkedinLoginAction = () => {
        window.open(`https://www.linkedin.com/oauth/v2/authorization?client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=${responseType}`)
    }

也许是用户配置文件与电子邮件地址隐私政策链接的一些问题或什么?

vc6uscn9

vc6uscn91#

我通过添加queryParams的方法而不是直接在url中修复它:

webClient.get()
            .uri(uriBuilder -> uriBuilder.path("v2/emailAddress")
                .queryParam("q", "members")
                .queryParam("projection", "(elements*(handle~))").build())
            .header("Authorization", "Bearer " + facebook.getAccessToken())
            .retrieve()
            .bodyToMono(GoogleProfile.class)
            .log()
            .block();
tp5buhyn

tp5buhyn2#

一种更简单、更好的方法是使用userinfo端点,如此处所述。
这将在同一个响应中为您提供所有必填字段:

{
"sub": "782bbtaQ",
"name": "John Doe",
"given_name": "John",
"family_name": "Doe",
"picture": "https://media.licdn-ei.com/dms/image/C5F03AQHqK8v7tB1HCQ/profile-displayphoto-shrink_100_100/0/",
"locale": "en-US",
"email": "doe@email.com",
"email_verified": true

}

相关问题