Automatically posting articles from dev.to to linkedin.com

Serhii Korol - Oct 23 - - Dev Community

Hi authors, this article is dedicated to expanding the coverage of your articles. Almost every developer has a LinkedIn account, and this article is for you.

LinkedIn

Unfortunately, the LinkedIn API doesn't allow posting publications directly as a simple user. You need to create a company for this. First of all, go to the business page and make the new company's page using this link.

business

You'll be redirected to the new page for the business of your page:

company

Fill required fields:

company form

If you everything done is correct, you'll be redirected to the company's page:

company page

Now, you need to go to the developer portal using link and create the new application:

portal

Fill up all required fields and find your company:

app form

You'll be redirected to the application page, where you must request access to two products. Without them, you won't be able to use the API.

request

If you click on the endpoints, you'll see available endpoints.

endpoints

You have access to endpoints as a member of LinkedIn.

all endpoints

The next step is to verify your app:

verify

There will be a generated link you should go to to associate your company and application.

bind

In the last step, you need to go by the link and generate an access token:

token

You need to select your app and scope:

token form

In the throws pop-up window, enter login and password to your LinkedIn account:

auth

If everything is correct, you'll see the token. Save it.

generated token

dev.to

Go to the https://dev.to/settings/extensions and generate the key for API.

extensions

GitHub

Next, we need to add a job to the GitHub repository. I won't tell you how to create the public repository. You can find it on this site. As an example, you can also explore my repository. The workflow should contain a schedule like this.

name: "Cronjob"
on:
  schedule:
    - cron: '15 */6 * * *'
  push:
    branches:
      - 'main'
Enter fullscreen mode Exit fullscreen mode

In the jobs namespace, add the new job like this:

- name: Post published articles to LinkedIn
        run: |
          git clone https://github.com/SergKorol/PostDevToLi.git
          cd PostDevToLi/PostDevToLi
          dotnet build
          if [ -f ../../data/posted_articles.db ]; then
              cp ../../data/posted_articles.db ./bin/Debug/net8.0/
              if [ $? -ne 0 ]; then
              echo "Error: Failed to copy the database."
              exit 1
              fi
          fi

          dotnet restore
          dotnet run --project PostDevToLi.csproj -- \
          --api-key="${{ secrets.DEV_TO_API_KEY }}" \
          --access-token="${{ secrets.ACCESS_TOKEN }}" \
          --ago=72
      - name: Copy DB to the correct folder
        run: |
          cp PostDevToLi/PostDevToLi/bin/Debug/net8.0/posted_articles.db ./data/posted_articles.db
Enter fullscreen mode Exit fullscreen mode

I'll tell you a little bit about the application. It's a simple console application that gets three arguments. The first two are tokens to API, dev.to, and linkenin.com. The last argument is the hours count from the current DateTime, which we use to take and publish articles. If the article is already published, it won't published again. And the critical notice. The application takes only the last 30 published articles. You have published many articles if you haven't posted anything on LinkedIn. Some of them won't be posted on LinkedIn. The service is oriented toward regular postings on LinkedIn. In this case, you won't have any issues.

I hope the article was helpful to you. See you next week. Happy coding!

Buy Me A Beer

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .