<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: OAuth U2M Manual token generation failing in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80732#M36144</link>
    <description>&lt;P&gt;No problem, glad that I could help. You can also marked my answer as a solution to help community&lt;/P&gt;</description>
    <pubDate>Fri, 26 Jul 2024 15:52:18 GMT</pubDate>
    <dc:creator>szymon_dybczak</dc:creator>
    <dc:date>2024-07-26T15:52:18Z</dc:date>
    <item>
      <title>OAuth U2M Manual token generation failing</title>
      <link>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/65214#M32752</link>
      <description>&lt;P&gt;I am writing a frontend webpage that will log into DataBricks and allow the user to select datasets.&lt;/P&gt;&lt;P&gt;I am new to front end development, so there may be some things I am missing here, but I know that the DataBricks SQL connector for javascript only works with Node, which does not work client side. I am aware that there are ways to shoehorn node packages into running in the browser but I am trying to avoid this.&lt;/P&gt;&lt;P&gt;I am looking into using OAuth U2M with manual token generation, as outlined in this article&amp;nbsp;&lt;A href="https://learn.microsoft.com/en-us/azure/databricks/dev-tools/auth/oauth-u2m#--manually-generate-and-use-access-tokens-for-oauth-user-to-machine-u2m-authentication" target="_self"&gt;https://learn.microsoft.com/en-us/azure/databricks/dev-tools/auth/oauth-u2m#--manually-generate-and-use-access-tokens-for-oauth-user-to-machine-u2m-authentication&lt;/A&gt;&lt;BR /&gt;I am able to generate the authorization code in the browser URL just fine (Step 2). However, when I get to the step of generating an OAuth access token (Step3:&amp;nbsp;&lt;A href="https://learn.microsoft.com/en-us/azure/databricks/dev-tools/auth/oauth-u2m#--step-3-use-the-authorization-code-to-generate-an-oauth-access-token" target="_self"&gt;https://learn.microsoft.com/en-us/azure/databricks/dev-tools/auth/oauth-u2m#--step-3-use-the-authorization-code-to-generate-an-oauth-access-token&lt;/A&gt;), the POST request always returns a 400 with a response of&amp;nbsp;{"error":"invalid_request","error_description":"Invalid authorization code"}&lt;/P&gt;&lt;P&gt;This post request I am doing in python, just as a proof of concept, but I would implement the whole system client side in javascript.&lt;/P&gt;&lt;P&gt;Here is the script I am using for the proof of concept. Any advice on why I keep receiving a 400 would be appreciated. Thanks!&lt;/P&gt;&lt;PRE&gt;import uuid, hashlib, base64, requests, json, webbrowser&lt;BR /&gt;# Generate a UUID.&lt;BR /&gt;uuid1 = uuid.uuid4()&lt;BR /&gt;# Convert the UUID to a string.&lt;BR /&gt;uuid_str1 = str(uuid1).upper()&lt;BR /&gt;# Create the code verifier.&lt;BR /&gt;code_verifier = uuid_str1 + "-" + uuid_str1&lt;BR /&gt;# Create the code challenge based on the code verifier.&lt;BR /&gt;code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode()).digest()).decode('utf-8')&lt;BR /&gt;# Remove all padding from the code challenge.&lt;BR /&gt;code_challenge = code_challenge.replace('=', '')&lt;BR /&gt;print(f"code_verifier: {code_verifier}")&lt;BR /&gt;print(f"code_challenge: {code_challenge}")&lt;BR /&gt;account_id = "**** MY ACCOUNT ID ****"&lt;BR /&gt;&lt;BR /&gt;redirect_url = "http://localhost:8020"&lt;BR /&gt;url1 = f"https://accounts.azuredatabricks.net/oidc/accounts/{account_id}/v1/authorize?client_id=databricks-cli&amp;amp;redirect_url={redirect_url}&amp;amp;response_type=code&amp;amp;state=helloworld&amp;amp;code_challenge={code_challenge}&amp;amp;code_challenge_method=S256&amp;amp;scope=all-apis+offline_access"&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;webbrowser.open(url1, new=2, autoraise=True)&lt;BR /&gt;################### Get code from browser&lt;BR /&gt;&lt;BR /&gt;auth_code = "********" # code from browser&lt;BR /&gt;body = {"client_id":"databricks-cli","grant_type":"authorization_code","scope":"all-apis offline_access",&lt;BR /&gt;"redirect_uri":redirect_url, "code_verifier":code_verifier,"code":auth_code}&lt;BR /&gt;url2 = f"https://accounts.azuredatabricks.net/oidc/accounts/{account_id}/v1/token"&lt;BR /&gt;r = requests.post(url=url2, data=body)&lt;BR /&gt;&lt;BR /&gt;#This returns&lt;BR /&gt;#&amp;lt;Response [400]&amp;gt;&lt;BR /&gt;#b'{"error":"invalid_request","error_description":"Invalid authorization code"}'&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2024 18:44:44 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/65214#M32752</guid>
      <dc:creator>BenDataBricks</dc:creator>
      <dc:date>2024-04-01T18:44:44Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth U2M Manual token generation failing</title>
      <link>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80660#M36110</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/9"&gt;@Retired_mod&lt;/a&gt;&amp;nbsp;I am facing the same issue even though ensured all criteria you mentioned previously.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2024 06:13:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80660#M36110</guid>
      <dc:creator>MaheshMandlik</dc:creator>
      <dc:date>2024-07-26T06:13:56Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth U2M Manual token generation failing</title>
      <link>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80681#M36118</link>
      <description>&lt;P&gt;Followed this guide thoroughly&amp;nbsp;&lt;A href="https://docs.databricks.com/en/dev-tools/auth/oauth-u2m.html#manually-generate-and-use-access-tokens-for-oauth-user-to-machine-u2m-authentication" target="_blank"&gt;https://docs.databricks.com/en/dev-tools/auth/oauth-u2m.html#manually-generate-and-use-access-tokens-for-oauth-user-to-machine-u2m-authentication&lt;/A&gt;&lt;BR /&gt;Unfortunately, no luck. Surprisingly, it worked well when was used in the month of March 2024.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2024 08:41:05 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80681#M36118</guid>
      <dc:creator>MaheshMandlik</dc:creator>
      <dc:date>2024-07-26T08:41:05Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth U2M Manual token generation failing</title>
      <link>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80699#M36124</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/113799"&gt;@MaheshMandlik&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;I've got it working. I recommend to split your code into two files, it's a lot easier to test. Once you obtained authorization token, you need to act fast if you want to generate access token, because authorization token are short-lived:&lt;BR /&gt;&lt;BR /&gt;-&amp;nbsp; get_authorization_token.py&lt;BR /&gt;-&amp;nbsp; get_access_token.py&lt;BR /&gt;&lt;BR /&gt;In get_authorization_token.py:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import uuid
import hashlib
import base64
import requests
import json
import webbrowser


# Generate a UUID.
uuid1 = uuid.uuid4()

# Convert the UUID to a string.
uuid_str1 = str(uuid1).upper()

# Create the code verifier.
code_verifier = uuid_str1 + "-" + uuid_str1

# Create the code challenge based on the code verifier.
code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode()).digest()).decode('utf-8')

# Remove all padding from the code challenge.
code_challenge = code_challenge.replace('=', '')

print(f"code_verifier: {code_verifier}")
print(f"code_challenge: {code_challenge}")

account_id = "YOUR_ACCOUNT_ID"
redirect_url = "http://localhost:8020"
url1 = f"https://accounts.azuredatabricks.net/oidc/accounts/{account_id}/v1/authorize?client_id=databricks-cli&amp;amp;redirect_url={redirect_url}&amp;amp;response_type=code&amp;amp;state=helloworld&amp;amp;code_challenge={code_challenge}&amp;amp;code_challenge_method=S256&amp;amp;scope=all-apis+offline_access"  

webbrowser.open(url1, new=2, autoraise=True)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In get_access_token.py:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import uuid
import hashlib
import base64
import requests
import json
import webbrowser


# Generate a UUID.
uuid1 = uuid.uuid4()

# Convert the UUID to a string.
uuid_str1 = str(uuid1).upper()



account_id = "YOUR_ACCOUNT_ID"
redirect_url = "http://localhost:8020"


authorization_code = "YOUR_AUTHORIZATION_CODE FROM FIRST FILE"

code_verifier = "CODE VERIFIER FROM FIRST FILE"

url = f"https://accounts.azuredatabricks.net/oidc/accounts/{account_id}/v1/token"
data = {
    "client_id": "databricks-cli",
    "grant_type": "authorization_code",
    "scope": "all-apis offline_access",
    "redirect_uri": redirect_url,
    "code_verifier": code_verifier,
    "code": authorization_code
}

response = requests.post(url, data=data)

print(response.status_code)
print(response.json())&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2024 11:14:05 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80699#M36124</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2024-07-26T11:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth U2M Manual token generation failing</title>
      <link>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80714#M36133</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/113799"&gt;@MaheshMandlik&lt;/a&gt;, probably forgot to mention, but splitting this code to two separate files helps to avoid generating new code_verifier by mistake.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2024 12:03:07 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80714#M36133</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2024-07-26T12:03:07Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth U2M Manual token generation failing</title>
      <link>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80729#M36142</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/110502"&gt;@szymon_dybczak&lt;/a&gt;&amp;nbsp;Thank you for your help. Your solution has worked very well for me.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2024 13:46:51 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80729#M36142</guid>
      <dc:creator>MaheshMandlik</dc:creator>
      <dc:date>2024-07-26T13:46:51Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth U2M Manual token generation failing</title>
      <link>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80732#M36144</link>
      <description>&lt;P&gt;No problem, glad that I could help. You can also marked my answer as a solution to help community&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2024 15:52:18 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/oauth-u2m-manual-token-generation-failing/m-p/80732#M36144</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2024-07-26T15:52:18Z</dc:date>
    </item>
  </channel>
</rss>

