How to post on linkedin company api?

Viewed 187

I am following documentation to post on my company linkedin page (i am superadministrator) so i used GET https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id={your_client_id}&redirect_uri={your_callback_url}&state=foobar&scope=r_liteprofile%20r_emailaddress%20w_member_social to get code then I got access_token using this https://linkedin.com/oauth/v2/accessToken?client_id={client_id}&client_secret={client_Secret}&grant_type=authorization_code&redirect_uri={url}&code={code} Also i got approved on Marketing program so I dont have any problem at this point but When i try to post using credentials i get nothing in console and nothing appears on my company page. Here is my code I have a json with creds gotten from api

{
    "client_id": "",
    "client_secret": "",
    "redirect_uri": "https://www.exampleuri.com",
    "access_token": "",
    "page_id":"73482979"
}

Then read json

def headers(access_token):

    headers = {
    'Authorization': f'Bearer {access_token}',
    'cache-control': 'no-cache',
    'X-Restli-Protocol-Version': '2.0.0'
    }
    return headers

def auth(credentials):

    creds = read_creds(credentials)
    #print(creds)
    client_id, client_secret = creds['client_id'], creds['client_secret']
    redirect_uri = creds['redirect_uri']
    api_url = 'https://www.linkedin.com/oauth/v2'
         
    if 'access_token' not in creds.keys(): 
        args = client_id,client_secret,redirect_uri
        auth_code = authorize(api_url,*args)
        access_token = refresh_token(auth_code,*args)
        creds.update({'access_token':access_token})
        save_token(credentials,creds)
    else: 
        access_token = creds['access_token']
    return access_token

if __name__ == '__main__':
    credentials = 'credentials.json'
    access_token = auth(credentials)

Finally call functions above

import requests
from linked_credentials import auth,headers

credentials = 'credentials.json'
access_token = auth(credentials)
headers = headers(access_token)

def user_info(headers):

    response = requests.get('https://api.linkedin.com/v2/organizations', headers = headers)
    user_info = response.json()
    return user_info
 
# Get user id to make a UGC post
user_info = user_info(headers)
urn = user_info['page_id']

api_url = 'https://api.linkedin.com/v2/ugcPosts'
author = f'urn:li:organization:{urn}'
message = '''
Testing post on LinkedIn with python
'''
link = 'https://www.example.com/'
link_text = 'Example!'

post_data = {
    "owner": author,
        "lifecycleState": "PUBLISHED",
        "specificContent": {
            "com.linkedin.ugc.ShareContent": {
                "shareCommentary": {
                    "text": message
                },
                "shareMediaCategory": "ARTICLE",
                "media": [
                    {
                        "status": "READY",
                        "description": {
                            "text": message
                        },
                        "originalUrl": link,
                        "title": {
                            "text": link_text
                        }
                    }
                ]
            }
        },
        "visibility": {
            "com.linkedin.ugc.MemberNetworkVisibility": "CONNECTIONS"
        }
    }
 
if __name__ == '__main__':
    r = requests.post(api_url, headers=headers, json=post_data)
    r.json()

This is what i have in my app application

0 Answers
Related