Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix LinkedIn get_user() and create_post() method. #397

Merged
merged 2 commits into from
Feb 3, 2025

Conversation

tybalex
Copy link
Contributor

@tybalex tybalex commented Feb 3, 2025

What does this PR do:

@tybalex tybalex self-assigned this Feb 3, 2025
@@ -97,10 +96,14 @@ async def create_post(client: RestliClient):
raise ValueError(
f"Error: failed to create post. Status code: {response.status_code}. Response: {response.entity}"
)

response.entity["link"] = (
f"https://www.linkedin.com/feed/update/{response.entity['id']}" # craft the link manually
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the link to post created

response = client.get(resource_path="/userinfo", access_token=ACCESS_TOKEN)
def get_user(client: RestliClient) -> dict:

MAX_RETRIES = 3
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 retries to prevent a weird 401 error from the Linkedin API side.

MAX_RETRIES = 3
DELAY = 1 # seconds between retries

for attempt in range(1, MAX_RETRIES + 1):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems strange to me that we would need to retry this, Especially for a 401 error.

Copy link
Contributor Author

@tybalex tybalex Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I’m with you on this. Here’s the issue in detail:

Sometimes, the first time the tool makes a get_user() request, it gets an error response like this:

{
  "status": 401,
  "serviceerrorcode": 65601,
  "code": "revoked_access_token"
}

However, the second time it works just fine.

My theory: The OAuth process in the LinkedIn API server might be slow sometimes. After the access token is granted, the get_user() request might hit the server before the token is marked as valid in their system.

Another possible fix: I tried adding a time.sleep(2) at the beginning of the function, and it worked. But a retry mechanism would likely make more sense.

@tybalex tybalex merged commit c3a4748 into obot-platform:main Feb 3, 2025
1 check passed
@tybalex tybalex deleted the fix-linkedin branch February 3, 2025 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants