如何通过 OAuth 通过 CData ODBC 驱动程序对 Salesforce 进行身份验证?

How to authenticate Salesforce through CData ODBC driver via OAuth?

我已经为 Salesforce 安装了 CData ODBC 驱动程序,并且能够通过用户名、密码和安全令牌连接 Salesforce。我也想通过 OAuth 访问。我已按照下面 link 中提到的所有步骤进行操作。我们怎样才能走得更远。

http://cdn.cdata.com/help/RFE/odbc/pg_oauth.htm

这是我的代码,用于通过用户名、密码和安全令牌连接 Salesforce,并能够将数据保存在 csv 文件中。如何通过 OAuth 做同样的事情?

import pyodbc
import csv
cnxn = pyodbc.connect("DRIVER={CData ODBC Driver for Salesforce};User=yourusername;Password=password;Security Token=security token;")
cursor = cnxn.cursor()
query = "SELECT * from AccountPartner"
cursor.execute(query)
csvfile=open('persons.csv','w', newline='')
obj=csv.writer(csvfile)
for row in cursor:
    print(row)
    obj.writerow(row)
csvfile.close()

您需要的说明在文档的下方(也在下面复制):http://cdn.cdata.com/help/RFE/odbc/pg_oauthcustomappcreate.htm

您需要在 Salesforce 中创建自定义应用程序,然后在您的连接字符串中设置规定的 OAuth 相关连接属性。您创建连接的代码如下所示:

cnxn = pyodbc.connect("DRIVER={CData ODBC Driver for Salesforce};OAuthClientID=MY_CONSUMER_KEY;OAuthClientSecret=MY_CONSUMER_SECRET;OAuthCallbackURL=https://localhost:33333;InitiateOAuth=GETANDREFRESH;")

您可能需要先测试连接并在 Python 之外触发 OAuth 流程。该过程的说明可以在帮助 (http://cdn.cdata.com/help/RFE/odbc/pg_unixODBConlinux.htm) 的 Unix ODBC 部分找到,但也复制在下面。


从文档中复制的说明

Create a Connected App

To obtain the OAuth client credentials, consumer key, and consumer secret:

  1. Log in to Salesforce.com.
  2. From Setup, enter Apps in the Quick Find box and then click the link to create an app.
  3. In the Connected Apps section of the resulting page, click New. Enter a name to be displayed to users when they log in to grant permissions to your app, along with a contact Email address.
  4. Click Enable OAuth Settings and enter a value in the Callback URL box. If you are making a desktop application, set the Callback URL to http://localhost:33333 or a different port number of your choice. If you are making a web application, set the Callback URL to a page on your Web app you want the user to be returned to after they have authorized your application.
  5. Select the scope of permissions that your app should request from the user.
  6. Click your app name to open a page with information about your app. The OAuth client credentials, the consumer key, and consumer secret are displayed.

Authenticate to Salesforce from a Desktop Application

After setting the following connection properties, you are ready to connect:

  • OAuthClientId: Set to the consumer key in your app settings.
  • OAuthClientSecret: Set to the consumer secret in your app settings.
  • CallbackURL: Set to the callback URL in your app settings.
  • InitiateOAuth: Set to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken connection property.

When you connect, the driver opens the OAuth endpoint in your default browser. Log in and grant permissions to the application. The driver then completes the following OAuth process:

  1. Gets the callback URL and sets the access token and OAuthServerUrl to authenticate requests.
  2. Saves OAuth values in OAuthSettingsLocation to be persisted across connections.
  3. Exchanges the returned refresh token for a new, valid access token.

Using unixODBC

Define a DSN

Create a DSN by adding an entry to odbc.ini or .odbc.ini. Below is an example DSN entry:

[CData Salesforce Source]
Driver=/opt/cdata/cdata-odbc-driver-for-salesforce/lib/libsalesforceodbc.x64.so
OAuthClientID=MY_CONSUMER_KEY
OAuthClientSecret=MY_CONSUMER_SECRET
OAuthCallbackURL=https://localhost:33333
InitiateOAuth=GETANDREFRESH

Test the Connection

You can use the unixODBC test tool, isql, to execute SQL queries to Salesforce from the command line. When testing the connection, use the -v flag to output any messages from the driver manager and the driver.

isql -v "CData Salesforce Source"

在此处输入代码在您的连接字符串中进行如下更改

cnxn = pyodbc.connect("DRIVER={CData ODBC DRIVER for
Salesforce};InitiateOAuth='GETANDREFRESH';OAuthClientId='myclient_id';OAuthClientSecret='my_secret';callbackurl='my_redirect_uri';oauthaccesstoken='my_access_token';oauthserverurl='server_url_returned_in_oauth';OAuthRefreshToken='refresh_token';")