[原文網址](https://connect.spotware.com/docs/open_api_2/getting_started_v2/open_authentication_v2)
Spotware Open API身份驗證過程是基于OAuth 2.0設計的。 OAuth 2.0授權框架允許第三方應用程序通過編排資源所有者和服務之間的批準交互,或者通過允許第三方應用程序獲取訪問權來代表資源所有者獲得對服務的有限訪問權限 它代表自己。
與資源(例如交易賬戶)所有者的通信如下:
1.交易帳戶所有者決定通過點擊您網站上的某個操作按鈕開始使用您的服務。
????
2.該服務生成對Open API 2.0的請求,將交易者重定向到Spotware端。
????
3.交易者使用他的個人cTrader個人資料登錄并允許權限。
????
4.之后,交易者被重定向回服務端,其身份驗證碼可以與訪問令牌交換。 訪問令牌允許服務代表交易者使用交易賬戶執行操作。
#### Application Authorization
為了獲得對交易賬戶信息和交易的訪問權限,您應該為使用您的應用程序的每個cTrader ID擁有一個身份驗證令牌。
如果要獲取cTrader ID的身份驗證令牌,則應按照中所述的說明進行操作[OAuth Services Description](https://connect.spotware.com/docs/api-reference/oauth-services-description)section of[API Reference](https://connect.spotware.com/docs/api-reference).
但是,為了在跳過身份驗證流程的同時測試我們的Open API,您可以按照中所述的步驟進行操作[Playground](https://connect.spotware.com/docs/available-resources/playground)部分。 我們將在演示連接站點注冊的測試應用程序“Test Partner”請求此身份驗證令牌。
從“應用程序”部分選擇您的應用程序,然后單擊旁邊的“操作”。 選擇您需要的范圍 - 交易或帳戶 - 然后單擊獲取令牌。
**Note 1.*如果您不知道cTrader ID是什么,為什么需要它以及它如何工作,請檢查[the corresponding section](https://connect.spotware.com/docs/available-resources/playground#ctraderID)下面。 因為您必須使用OAuth服務。
**Note 2.**如果您不了解OAuth技術,請查看OAuth標準參考[RFC6749: The OAuth 2.0 Authorization Framework](http://tools.ietf.org/html/rfc6749).
**Authorization Flow**
激活新添加的應用程序后,您可以繼續獲取訪問令牌并授權帳戶。
單擊所需應用程序旁邊的Playground。 在這里,您可以找到您的訪問權限并刷新令牌并授權帳戶。 選擇權限范圍,然后單擊“獲取令牌”。
請注意,OAuth 2.0請求有兩種權限范圍:
*賬戶 - 要求訪問賬戶信息(交易歷史,頭寸,掛單等)
????
*交易 - 要求代表賬戶所有者執行交易操作的權限(發送市場和掛單,接收交易事件等)

隱藏或顯示客戶端cTrader ID并通過電子郵件發送給應用程序,選擇要為其啟用身份驗證的帳戶,然后單擊“允許訪問”。

API使用[cTrader ID](https://help.spotware.com/ctid)作為交易者的個人資料標識符登錄cTrader平臺環境。有關更多詳細信息,請查看官方OAuth 2.0授權框架參考:[RFC6749 OAuth 2.0授權框架](https://tools.ietf.org/html/rfc6749)
#### Using OAuth examples
**Note.**如果您打算使用OAuth庫來使用授權服務器,請記住該庫應該與OAuth v2 draft 20兼容。
提供兩種服務:
* Auth(例如https://sandbox-connect.spotware.com/apps/auth) - 可用于授權資源(交易賬戶)所有者。
????
*令牌(例如https://sandbox-connect.spotware.com/apps/token) - 可用于刷新或獲取令牌。
#### Obtaining and refreshing access token workflow:
Getting Authorization code URI:
~~~
https://sandbox-connect.spotware.com/apps/auth?
client_id={your Partner's Id here}&
redirect_uri={redirect_uri}&
scope=accounts
~~~
*Example:*
~~~
https://sandbox-connect.spotware.com/apps/auth?client_id=7_5az7pj935owsss8kgokcco84wc8osk0g0gksow0ow4s4ocwwgc&redirect_uri=http%3A%2F%2Fwww.spotware.com%2F&scope=accounts
~~~
Exchanging Authorization code with access\_token and refresh\_token:
~~~
https://sandbox-connect.spotware.com/apps/token?
grant_type=authorization_code&
code={authorization_code}&
redirect_uri={redirect_uri}&
client_id={your Partner's Id here}&
client_secret={your Partner's Secret here}
~~~
**Note.***{authorization\_code}*應該使用實際的授權代碼替換占位符以獲取授權代碼響應。
*Example:*
~~~
https://sandbox-connect.spotware.com/apps/token?grant_type=authorization_code&code={authorization_code}&redirect_uri=http%3A%2F%2Fwww.spotware.com%2F&client_id=7_5az7pj935owsss8kgokcco84wc8osk0g0gksow0ow4s4ocwwgc&client_secret=49p1ynqfy7c4sw84gwoogwwsk8cocg8ow8gc8o80c0ws448cs4
~~~
**Note.**請記住,訪問令牌有到期時間。 當您收到有關訪問令牌過期的錯誤消息時,您應該生成消息以使用刷新令牌請求刷新最后一個消息。 請參閱下面的詳細信息。
Refreshing token URI:
~~~
https://sandbox-connect.spotware.com/apps/token?
grant_type=refresh_token&
refresh_token={refresh_token}&
client_id={your Partner's Id here}&
client_secret={your Partner's Secret here}
~~~
**Note.***{refresh\_token}*占位符應替換為實際刷新令牌。
*Example:*
~~~
https://sandbox-connect.spotware.com/apps/token?grant_type=refresh_token&refresh_token={refresh_token}&client_id=7_5az7pj935owsss8kgokcco84wc8osk0g0gksow0ow4s4ocwwgc&client_secret=49p1ynqfy7c4sw84gwoogwwsk8cocg8ow8gc8o80c0ws448cs4
~~~
**Note.**在開始使用OAuth服務之前,請閱讀本指南的以下部分:[OAuth Services Description](https://connect.spotware.com/docs/api-reference/oauth-services-description)for more details.
Get access to accounts via OAuth
當交易者決定允許第三方應用程序訪問其交易賬戶時,他們應該被帶到Open API OAuth授權服務器。 交易員將進入他的[cTrader ID](http://help.spotware.com/ctid)要登錄的憑據,然后將詢問他想要授予哪種資源。 獲取權限后,OAuth后端會生成與允許的資源相關的訪問令牌。 然后將訪問令牌發送回第三方的后端。
在第三方的應用程序獲得訪問令牌后,它可以使用交易者的所有授權資源,而無需任何其他請求。
- 空白目錄
- API Reference 2.0
- Getting Started
- Open Authentication
- Protocol Buffers
- Protobuf Messages Reference
- Open API Messages
- Open API Model Messages
- Open API Error Codes
- Example Projects .NET
- Spotware Proxy Cloud
- Getting Started in C#
- Test Environment
- Playground
- Frequently Asked Questions
- Lesson 1. Your First App
- Lesson 2. Get Trading Data
- Lesson 3. Get Market Data
- Lesson 4. Using Trading API