LinkedIn领英官方API接口文档说明:个人页/主页分享图文

@高效码农  August 8, 2019

由于Linkedin官方API升级和调整:API不对国内用户提供公司页面的图文分享

书接上文:LinkedIn领英官方API接口文档说明:授权代码流程

获取Token

获取到token后将token持久化到数据库,方便随时调用(token获取伪代码如下)

     /**
     * linkedin回调函数
     *
     * @param code
     * @return
     */
    public boolean index(String code, String mark) {
        String client_id = PropKit.get("linkedin_client_id");
        String client_secret = PropKit.get("linkedin_client_secret");
        String redirect_uri = PropKit.get("linkedin_redirect_uri");
        String url = "https://www.linkedin.com/oauth/v2/accessToken";
        String param = "grant_type=authorization_code&" +
                "code=" + code +
                "&redirect_uri=" + redirect_uri +
                "&client_id=" + client_id +
                "&client_secret=" + client_secret;

        //发送POST请求:使用授权代码交换访问令牌
        String result = HttpRequest.sendPost(url, param);

        JSONObject resultJson = JSON.parseObject(result);
        String token = resultJson.getString("access_token");
        boolean success = saveLinkedin(token, mark);
        return success;
    }

获取个人信息

分享图文要用到ID,同时还想获取Linkedin的头像等信息:

 /**
     * 获取Linkedin账号信息
     *
     * @param token
     * @param mark
     * @return
     */
    public boolean saveLinkedin(String token, String mark) {
        String url = "https://api.linkedin.com/v2/me";
        String result = HttpRequest.sendGet(url, "projection=(id,localizedFirstName,localizedLastName,profilePicture(displayImage~:playableStreams))", token);
        JSONObject resultJson = JSON.parseObject(result);

        String uuid = CommonUtils.randomID();
        int socialType = 2;
        String socialId = resultJson.getString("id");
        String socialPicture = resultJson.getJSONObject("profilePicture")
                .getJSONObject("displayImage~")
                .getJSONArray("elements")
                .getJSONObject(1)
                .getJSONArray("identifiers")
                .getJSONObject(0)
                .getString("identifier");
        String socialName = resultJson.getString("localizedFirstName") + resultJson.getString("localizedLastName");
}

返回的json(节选):

{
    "localizedLastName": "XXX",
    "profilePicture": {
        "displayImage": "urn:li:digitalmediaAsset:XXX",
        "displayImage~": {
            "paging": {
                "count": 10,
                "start": 0,
                "links": []
            },
            "elements": [
                {
                    "artifact": "urn:li:digitalmediaMediaArtifact:(urn:li:digitalmediaAsset:XXX,urn:li:digitalmediaMediaArtifactClass:profile-displayphoto-shrink_800_800)",
                    "authorizationMethod": "PUBLIC",
                    "data": {
                        "com.linkedin.digitalmedia.mediaartifact.StillImage": {
                            "storageSize": {
                                "width": 800,
                                "height": 800
                            },
                            "storageAspectRatio": {
                                "widthAspect": 1,
                                "heightAspect": 1,
                                "formatted": "1.00:1.00"
                            },
                            "mediaType": "image/jpeg",
                            "rawCodecSpec": {
                                "name": "jpeg",
                                "type": "image"
                            },
                            "displaySize": {
                                "uom": "PX",
                                "width": 800,
                                "height": 800
                            },
                            "displayAspectRatio": {
                                "widthAspect": 1,
                                "heightAspect": 1,
                                "formatted": "1.00:1.00"
                            }
                        }
                    },
                    "identifiers": [
                        {
                            "identifier": "XXX",
                            "file": "urn:li:digitalmediaFile:(urn:li:digitalmediaAsset:XXX,urn:li:digitalmediaMediaArtifactClass:profile-displayphoto-shrink_800_800,0)",
                            "index": 0,
                            "mediaType": "image/jpeg",
                            "identifierType": "EXTERNAL_URL",
                            "identifierExpiresInSeconds": 1570665600
                        }
                    ]
                }
            ]
        }
    },
    "id": "XXX",
    "localizedFirstName": "XXX"
}
其中localizedFirstName、localizedLastName为姓名,identifier为头像链接

主页图文分享

伪代码:

/**
     * linkedin主页分享
     *
     * @param comment
     * @param links
     * @return
     */
    public boolean share(UploadFile uploadFile, HttpServletRequest request, String id, String comment, String links) throws Exception {
        String url = "https://api.linkedin.com/v2/shares";
        String httpImgUrl = "";
        if (uploadFile != null) {
            httpImgUrl = CommonUtils.saveImage(uploadFile, request);
        }
        Socialmanager socialmanager = Socialmanager.dao.findByIdLoadColumns (id,"socialToken, socialId, socialName");
        String objectString = "{\n" +
                "    \"content\": {\n" +
                "        \"contentEntities\": [\n" +
                "            {\n" +
                "                \"entityLocation\": \""+ links +"\",\n" +
                "                \"thumbnails\": [\n" +
                "                    {\n" +
                "                        \"resolvedUrl\": \""+ httpImgUrl +"\"\n" +
                "                    }\n" +
                "                ]\n" +
                "            }\n" +
                "        ],\n" +
                "        \"title\":\""+ comment +"\"\n" +
                "    },\n" +
                "    \"distribution\": {\n" +
                "        \"linkedInDistributionTarget\": {}\n" +
                "    },\n" +
                "    \"owner\": \"urn:li:person:"+ socialmanager.getSocialId() +"\",\n" +
                "    \"subject\": \""+ comment +"\",\n" +
                "    \"text\": {\n" +
                "        \"text\": \""+ comment +"\"\n" +
                "    }\n" +
                "}";
        boolean sendResult = HttpRequest.sendPost2Linkedin(url, objectString, socialmanager.getSocialToken());
        return sendResult;
    }

参数json:

{
    "content": {
        "contentEntities": [
            {
                "entityLocation": "https://www.example.com/content.html",
                "thumbnails": [
                    {
                        "resolvedUrl": "https://www.example.com/image.jpg"
                    }
                ]
            }
        ],
        "title": "Test Share with Content"
    },
    "distribution": {
        "linkedInDistributionTarget": {}
    },
    "owner": "urn:li:person:324_kGGaLE",
    "subject": "Test Share Subject",
    "text": {
        "text": "Test Share!"
    }
}
官方文档对参数的解释有瑕疵,暂不对参数解释;按照格式上传即可

Linkedin V2权限、错误码相关文档地址:

权限:https://docs.microsoft.com/en-us/linkedin/shared/references/migrations/default-scopes-migration

错误码:https://docs.microsoft.com/en-us/linkedin/shared/api-guide/concepts/error-handling



评论已关闭

  1. Jell

    请问若要实现将网页文章分享到领英,前端可以单独实现吗?我经常分享出去领英提示找不到网页。

    1. @Jell

      可以,但是你每次都有获取授权