planningcenter / developers

Planning Center API docs and support
https://developer.planning.center/docs/
84 stars 8 forks source link

HTTP Basic: Access denied. #1133

Closed CuriousDataLearner closed 10 months ago

CuriousDataLearner commented 10 months ago

Related Product Which product is this question related to? People

Describe the question I have access to https://people.planningcenteronline.com/ and have read the documentation but can't figure out why i'm getting the access denied error.

What have you tried that worked? Nothing yet

What have you tried that didn't work?

app_id = 'not my app_id' secret = 'not my secret'

headers = { "Authorization": f"Bearer {app_id}:{secret}", } data_list = [] url = 'https://api.planningcenteronline.com/people/v2/people' response = requests.get(url, headers=headers)

if not response.ok: print(response.text) raise Exception("Failed to retrieve data from API")

data = response.json()

Additional context I'm new, but don't give up easy. God bless.

I have..

pastorhudson commented 10 months ago

Try Base64 encoding the app-id and secret


import base64

app_id = 'your_app_id'
secret = 'your_secret'

# Combine the app_id and secret
combined = f"{app_id}:{secret}"

# Encode the combined string
encoded_credentials = base64.b64encode(combined.encode()).decode()

# Creating the headers
headers = {
    "Authorization": f"Bearer {encoded_credentials}",
}
CuriousDataLearner commented 10 months ago

That solved that error. Thank you!

CuriousDataLearner commented 10 months ago

I'm sorry, i got an exception but didn't see the same HTTP Basic: Access denied error is still present.

import requests import jsonpickle import base64

app_id = 'not my app_id' secret = 'not my secret'

Combine the app_id and secret

combined = f"{app_id}:{secret}"

Encode the combined string

encoded_credentials = base64.b64encode(combined.encode()).decode()

Creating the headers

headers = { "Authorization": f"Bearer {encoded_credentials}", }

data_list = []

Adjust the URL to Planning Center API endpoint with the desired include parameter

url = 'https://api.planningcenteronline.com/people/v2/people'

url = 'https://api.planningcenteronline.com/people/v2/people?include=emails,phone_numbers'

response = requests.get(url, headers=headers)

if not response.ok: print(response.text) raise Exception("Failed to retrieve data from API")

data = response.json()

OUTPUT IS: raise Exception("Failed to retrieve data from API") Exception: Failed to retrieve data from API

seven1m commented 10 months ago

Can you share the error message you get in the response body?

CuriousDataLearner commented 10 months ago

this? PS C:\Users\matth\Python> c:; cd 'c:\Users\matth\Python'; & 'C:\Users\matth\Python\python.exe' 'c:\Users\matth.vscode\extensions\ms-python.python-2023.20.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '62223' '--' 'c:\Users\matth\Python\Planning Center V1-Source.py' HTTP Basic: Access denied.

Traceback (most recent call last): File "c:\Users\matth\Python\Planning Center V1-Source.py", line 32, in raise Exception("Failed to retrieve data from API") Exception: Failed to retrieve data from API PS C:\Users\matth\Python>

seven1m commented 10 months ago

Oh I think I see the problem... you used "Bearer" instead of "Basic". I think it should be:

headers = {
  "Authorization": f"Basic {encoded_credentials}",
}
CuriousDataLearner commented 10 months ago

Thank you! that was it, on with my journey. Y'all are very responsive and helpful. Have a blessed day.

pastorhudson commented 10 months ago

Oh I think I see the problem... you used "Bearer" instead of "Basic". I think it should be:


headers = {

  "Authorization": f"Basic {encoded_credentials}",

}

That was my bad! 🤦‍♂️

Mkyleran commented 8 months ago

Here is a more succinct solution I found in the Requests documentation.

api_base = 'https://api.planningcenteronline.com'
endpoint = '/people/v2/people'

 response = requests.get(
    url=api_base + endpoint,
    auth=(api_id, secret),
)

No encoding required.