how to set a facebook profile picture using the graph api

后端 未结 5 1258
半阙折子戏
半阙折子戏 2020-11-27 13:00

Is there any way to change the user\'s profile picture using the graph api?

I know you can\'t with the rest api (reference), but I could not find anything in the new

5条回答
  •  自闭症患者
    2020-11-27 13:16

    I had this problem too. I've managed to upload a profile picture using this endpoint.

    This link provides information about how to upload a profile picture

            private async uploadProfilePhoto(pageId:string, accessToken: string, photoUrl: string){
                let url = FACEBOOK_API_URL + `${pageId}/picture`+ 
                    `?access_token=${accessToken}`+
                    `&picture=${photoUrl}`;
                let response = null;
    
                try {
                 response = await axios({
                    method: 'post',
                    url:  url,
                }); 
                } catch (err) {
                    /**  Here this error occures but the profile image is still uploaded. 
                     * {message: 'Unsupported post request.', type: 'GraphMethodException', code: 100, fbtrace_id: 'AGhsadasdaiqyf_YHJaztdasdadG7'
                     */           
                }
    
                return response;
            }
    

    Then to get the id of the profile picture in case you need it you can perform a get request on this endpoint.

    const FACEBOOK_API_URL ='https://graph.facebook.com/v7.0/'
    let url = FACEBOOK_API_URL + `${pageId}/photos`+
                `?access_token=${accessToken}` +
                `&fields=picture`;
    

提交回复
热议问题