Exemplo n.º 1
0
        /// <summary>
        /// Request authorization (a token) for this client in the specified facade.
        /// </summary>
        /// <param name="label">The label of this token.</param>
        /// <param name="facade">The facade for which authorization is requested.</param>
        /// <returns>A pairing code for this client.  This code must be used to authorize this client at BitPay.com/api-tokens.</returns>
        public virtual async Task <PairingCode> RequestClientAuthorizationAsync(string label, Facade facade)
        {
            var token = new Token();

            token.Id     = _Auth.SIN;
            token.Guid   = Guid.NewGuid().ToString();
            token.Facade = facade.ToString();
            token.Count  = 1;
            token.Label  = label ?? "DEFAULT";

            var json     = JsonConvert.SerializeObject(token);
            var response = await PostAsync("tokens", json).ConfigureAwait(false);

            var tokens = await ParseResponse <List <Token> >(response).ConfigureAwait(false);

            // Expecting a single token resource.
            if (tokens.Count != 1)
            {
                throw new BitPayException("Error - failed to get token resource; expected 1 token, got " + tokens.Count);
            }

            _Auth.SaveToken(tokens[0].Facade, tokens[0].Value);

            return(new PairingCode(tokens[0].PairingCode));
        }
Exemplo n.º 2
0
            public AccessToken GetAccessToken(Facade requirement)
            {
                var token = _Tokens.TryGet(requirement.ToString());

                if (token == null && (requirement == Facade.User || requirement == Facade.PointOfSale))
                {
                    token = _Tokens.TryGet(Facade.PointOfSale.ToString()) ?? _Tokens.TryGet(Facade.Merchant.ToString());
                }
                return(token);
            }