通过 Coinbase iOS SDK 检索所有用户钱包

Retrieving All User Wallets through Coinbase iOS SDK

我最近一直在尝试使用 Coinbase iOS SDK,我在检索用户的以太坊、莱特币和比特币现金余额以及历史交易时遇到了问题。目前,我只能用比特币、美元和欧元做到这一点,这似乎与 Coinbase 提供的演示应用程序的行为一致。

我已经使用 OAuth2 在 Coinbase API 访问页面上配置了一个应用程序,生成的客户端 ID 和密码正在该应用程序中使用。

问题似乎源于我修改了 Coinbase iOS SDK 以允许我将帐户参数传递为 '全部'。我曾希望这能让我查看所有用户帐户(ETH、BTC、LTC 等)的详细信息,但是,在 Coinbase 对象上调用“getAccountsList”时,我只能获得 BTC、美元和欧元。

NSString *accessToken = [response objectForKey:@"access_token"];
Coinbase *client = [Coinbase coinbaseWithOAuthAccessToken:accessToken];

[client getAccountsList:^(NSArray *accounts, CoinbasePagingHelper *paging, NSError *error) {
    for (CoinbaseAccount *account in accounts) {
        // Only BTC, USD and EUR are in the accounts array at this point. 
    }
}];

这令人惊讶,因为权限请求页面正确地向用户询问了所有钱包,如下面的屏幕截图所示:

我怀疑解决此问题的方法是使用 API 密钥,因为您可以准确指定要授予访问权限的帐户。但是我打算分发该应用程序,因此不应使用此技术。

这是我发送的 URL 的示例:

https://www.coinbase.com/oauth/authorize?response_type=code&client_id=CLIENT_ID_GOES_HERE&account=all&scope=balance%20transactions%20user&redirect_uri=com.example-name.example-app.coinbase-oauth%3A%2F%2Fcoinbase-oauth

有谁知道我如何使用 OAuth 请求访问所有用户帐户并能够检索每个帐户的详细信息?我定义的范围是否以某种方式不正确?我能想到的唯一选择是逐一请求访问每个钱包并存储单独的访问令牌。但是,这不会是很好的用户体验。

谢谢!

首先,如果您没有,则需要 create an account on Coinbase

那么,请先在Coinbase digital api documentation上看一下,我同意你的看法,使用API获取数据可能更容易(如果目标帐户只是您自己的个人)帐户)

因为根据 Coinbase:

API Key authentication should only be used to access your own account or merchant orders. If your application requires access to other Coinbase users’ accounts, do not use API Key. To securely access other Coinbase users’ accounts, use Coinbase Connect (OAuth2)

你有两种可能:


  • 使用API

假设用户已根据 wallet permission documentation.

授予 wallet:accounts:read 到 API 密钥(允许您列出用户的帐户及其余额)

完成后,您可以使用 iOS 的官方 wallet client libraries - coinbase 可通过 CocoaPods 使用 - 通过将以下行添加到您的 Podfile: :

pod "coinbase-official"

  • 使用 OAuth2 协议

根据this

It is a slightly more complex integration than the API Key authentication method, but is more flexible. OAuth2 works well for web applications, as well as desktop and mobile apps.

你会在第一个coinbase-connect integrating documentation, and you may also take a look on the official OAuth2 protocol website中找到很多信息。

假设您可以使用 OAuth2,您还必须在请求数据之前请求用户授予您权限。

由于您需要访问用户钱包,您仍然需要请求访问令牌并在授权请求中添加一个 scope 参数(您的应用程序请求访问的权限(范围)的逗号分隔列表),如果您需要查看完整的范围列表,请自行参考 to this page.

所需范围与 API 方法相同:wallet:accounts:read,您的请求将如下所示:

GET https://www.coinbase.com/oauth/authorize?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URL&state=SECURE_RANDOM&scope=wallet:accounts:read

成功请求后,将在响应中返回一个有效的访问令牌(如下所示):

{
"access_token": "6915ab99857fec1e6f2f6c078583756d0c09d7207750baea28dfbc3d4b0f2cb80",
"token_type": "bearer",
"expires_in": 7200,
"refresh_token": "73a3431906de603504c1e8437709b0f47d07bed11981fe61b522278a81a9232b7",
"scope": "wallet:user:read wallet:accounts:read"

}

获得访问令牌后,如果将以下 header 添加到请求中,则可以进行与先前范围对应的任何 API 调用:

Authorization: Bearer 6915ab99857fec1e6f2f6c078583756d0c09d7207750baea28dfbc3d4b0f2cb80

最后,您可以参考 API reference documentation 查看所有可能的 API 调用和相关范围。


总而言之,您需要授予权限,然后列出用户帐户,然后您可能会得到任何account resource:

Account resource represents all of a user’s accounts, including bitcoin, bitcoin cash, litecoin and ethereum wallets, fiat currency accounts, and vaults.

此致

添加参数

account=all

到 oAuth 端点:https://coinbase.com/oauth/authorize?account=all&response_type=code.‌ ..

详情如下:https://developers.coinbase.com/docs/wallet/coinbase-connect/permissions

Coinbase Connect applications can request different access to user’s wallets. This access is defined by account parameter on OAuth2 authorization URL. Available options are:`

  • select - (default) Allow user to pick the wallet associated with the application
  • new - Application will create a new wallet (named after the application)
  • all - Application will get access to all of user’s wallets

仍然没有运气。

尝试添加参数 account=all。它让我可以访问所有钱包(与 op 完全相同)。但是,在代码中,我只能获取BTC Wallet、BTC Vault、EUR Wallet和新创建的BTC Wallet。新钱包是通过添加参数 account=new.

创建的

尝试添加参数 account_currency=BTC,ETH 并在 oAuth 授权上选择 ETH 钱包。 getAccountsList 是否返回了 0 个对象并且服务器没有错误。

尝试撤销我的 Coinbase 帐户中的所有 API 应用程序访问权限(设置 -> 安全)。

范围:balance transactions user

端点:.../oauth/authorize?account=all&response_type=code&client_id=%...

我认为 iOS SDK 需要更新。它仍然连接到旧的 API 版本。

我使用的是原始的 Coinbase SDK。没有叉子。相反,在 wallet:accounts:read 范围旁边,我还将 ["accounts": "all"] 作为元参数添加到 startAuthentication 方法。

和。我没有使用 getAccountList 方法,而是更通用的 .doGet 方法和 api v2 帐户端点(所以 coinbase.doGet("https://api.coinbase.com/v2/accounts", parameters: nil) {(response, error) -> Void in

这为我提供了所有钱包的帐户信息。不过在这种情况下,您确实需要对响应对象进行一些 json 处理。