How to implement the callback URL for 'Upwork' REST API?

To be able to run this code (search jobs from upwork.com):

#!/usr/bin/env python

import upwork
from upwork.routers.jobs import search

# https://developers.upwork.com/?lang=python#authentication_authorization-request
# https://www.upwork.com/developer/keys/apply
config = upwork.Config(
        {
            "client_id": <my_client_id>,
            "client_secret": <my_client_secret>,
            "redirect_uri": <my_redirect_uri>
        }
    )

client = upwork.Client(config)

try:
    config.token
except AttributeError:
    authorization_url, state = client.get_authorization_url()
    # cover "state" flow if needed
    authz_code = input(
        "Please enter the full callback URL you get "
        "following this link:\n{0}\n\n> ".format(authorization_url)
    )

    print("Retrieving access and refresh tokens.... ")
    token = client.get_access_token(authz_code)

params = {'q': 'php', 'title': 'Web developer'}
search.Api(client).find(params)

I need to define a callback URL.

How do I implement it?

Anyone have a or script to do it?

How you fetch (client side) the returned values from the API from your callback (server side)? Or you need a web service on your localhost? I can't host a web service on my localhost, because I don't have the hand on the network to forward to my private IP

Looks like it's easier to scrape the site than using API :/ What do you think?

Upvotes: 2

Views: 792

Answers (1)

ryaneatdinosaurs
ryaneatdinosaurs

Reputation: 113

I think you can achieve this with change your redirect_uri configuration with your actual callback URL. I'm not so familiar with php but how about implement the callback URL with flask?

the minimalistic setup should be like

from flask import Flask, request
import upwork

app = Flask(__name__)

client_id = "<my_client_id>"
client_secret = "<my_client_secret>"
redirect_uri = "<my_redirect_uri>"

config = upwork.Config(
    {
        "client_id": client_id,
        "client_secret": client_secret,
        "redirect_uri": redirect_uri
    }
)

@app.route('/callback')
def callback():
    # Get the authorization code from the request
    auth_code = request.args.get('code')
    client = upwork.Client(config)
    token = client.get_access_token(auth_code)
    return f"Access token: {token.access_token}\nRefresh token: {token.refresh_token}"

if __name__ == '__main__':
    app.run()

you can test it in your local machine by running the flask server, and after you got authorized, the Upwork client will be called your redirect_uri (in here it could be navigated to the http://127.0.0.1:5000/callback url) and the callback() method will be called.

If you wanted to get the returned response from your callback URL, you can fetch it with requests library like

import requests

response = requests.get("http://127.0.0.1:5000/callback")
if response.status_code != 200:
   response.raise_for_status()
else:
   access_token, refresh_token = response.text.strip().split('\n')
   print(f"Access token: {access_token}\n Refresh token: {refresh_token}"

please bear in mind, that you will need to modify this code based on your case and the structure of your own server-side endpoint and also the way you setup the Upwork configuration.

Upvotes: 1

Related Questions