I want to create a Python program, which will be deployed in a Docker container via a Google Cloud Run service, just allowing me to communicate with the LinkedIn API, and then go further into what the API offers.
after having performed the gcloud run deploy
command which allows me to deploy the image in the container of my Google Cloud Run service, I have this error:
ValueError: Please provide code or authorization_response parameters.
here is my directory tree containing all the files sent in the Docker container image:
- .dockerignore
- config (file with Client ID and Client Secret)
- Dockerfile
- main.py
- requirements.txt
main.py:
#!/usr/bin/python
# imports
from flask import Flask, redirect, request
from requests_oauthlib import OAuth2Session
from requests_oauthlib.compliance_fixes import linkedin_compliance_fix
import configparser
config = configparser.ConfigParser()
config.read("config")
client_id = config.get("config", "API_KEY")
client_secret = config.get("config", "API_SECRET")
authorization_base_url = 'https://www.linkedin.com/uas/oauth2/authorization'
token_url = 'https://www.linkedin.com/uas/oauth2/accessToken'
linkedin = OAuth2Session(client_id, redirect_uri='[URL_SERVICE_GOOGLE]/auth/linkedin/callback')
linkedin = linkedin_compliance_fix(linkedin)
authorization_url, state = linkedin.authorization_url(authorization_base_url)
# make sure to run this command before starting app or configure SSL (how to set inside app, for later)
SSL_ENV = 'export OAUTHLIB_INSECURE_TRANSPORT=1'
app = Flask(__name__)
@app.route('/')
def init():
# config
config = configparser.ConfigParser()
config.read("config")
client_id = config.get("config", "API_KEY")
client_secret = config.get("config", "API_SECRET")
authorization_base_url = 'https://www.linkedin.com/uas/oauth2/authorization'
token_url = 'https://www.linkedin.com/uas/oauth2/accessToken'
linkedin = OAuth2Session(client_id,redirect_uri='[URL_SERVICE_GOOGLE]/auth/linkedin/callback')
linkedin = linkedin_compliance_fix(linkedin)
authorization_url, state = linkedin.authorization_url(authorization_base_url)
# redirect to linked in to get code & session
return redirect(authorization_url)
@app.route('/auth/linkedin/callback')
def auth():
response_url = request.url
code = request.args.get('code')
# for some reason response_url is not getting #! values in url at the end; so use code
linkedin.fetch_token(token_url, client_secret=client_secret, code=code)
# Fetch a protected resource, i.e. user profile
people = linkedin.get('https://api.linkedin.com/v1/people/~?format=json')
return people.content
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
requirements.txt:
Flask==2.1.0
requests_oauthlib==1.3.0
configparser==5.2.0
Dockerfile:
# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.10-slim
# Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True
# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./
# Install production dependencies.
RUN pip install --no-cache-dir -r requirements.txt
# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
# Timeout is set to 0 to disable the timeouts of the workers to allow Cloud Run to handle instance scaling.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
Impossible to solve, I didn't find the solution myself.