1
0
mirror of https://github.com/taigrr/nats.docs synced 2025-01-18 04:03:23 -08:00

Update token.md

This commit is contained in:
Ginger Collison 2019-10-04 15:00:50 -05:00 committed by GitHub
parent b519bd8aa6
commit 84b03ccc50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,7 +12,64 @@ The code uses localhost:4222 so that you can start the server on your machine to
## Connecting with a Token ## Connecting with a Token
!INCLUDE "../../\_examples/connect\_token.html" {% tabs %}
{% tab title="Go" %}
```go
// Set a token
nc, err := nats.Connect("127.0.0.1", nats.Name("API Token Example"), nats.Token("mytoken"))
if err != nil {
log.Fatal(err)
}
defer nc.Close()
// Do something with the connection
```
{% endtab %}
{% tab title="Java" %}
```java
Options options = new Options.Builder().
server("nats://localhost:4222").
token("mytoken"). // Set a token
build();
Connection nc = Nats.connect(options);
// Do something with the connection
nc.close();
```
{% endtab %}
{% tab title="JavaScript" %}
```javascript
let nc = NATS.connect({url: `nats://127.0.0.1:${port}`, token: "mytoken!"});
```
{% endtab %}
{% tab title="Python" %}
```python
nc = NATS()
await nc.connect(servers=["nats://mytoken@demo.nats.io:4222"])
# Do something with the connection.
```
{% endtab %}
{% tab title="Ruby" %}
```ruby
NATS.start(token: "deadbeef") do |nc|
puts "Connected using token"
end
```
{% endtab %}
{% tab title="TypeScript" %}
```typescript
let nc = await connect({url: server.nats, token: "mytoken"});
```
{% endtab %}
{% endtabs %}
## Connecting with a Token in the URL ## Connecting with a Token in the URL
@ -22,5 +79,60 @@ Some client libraries will allow you to pass the token as part of the server URL
Again, once you construct this URL you can connect as if this was a normal URL. Again, once you construct this URL you can connect as if this was a normal URL.
!INCLUDE "../../\_examples/connect\_token\_url.html" {% tabs %}
{% tab title="Go" %}
```go
// Token in URL
nc, err := nats.Connect("mytoken@localhost")
if err != nil {
log.Fatal(err)
}
defer nc.Close()
// Do something with the connection
```
{% endtab %}
{% tab title="Java" %}
```java
Connection nc = Nats.connect("nats://mytoken@localhost:4222");//Token in URL
// Do something with the connection
nc.close();
```
{% endtab %}
{% tab title="JavaScript" %}
```javascript
let url = `nats://mytoken!@127.0.0.1:${port}`;
let nc = NATS.connect({url: url});
```
{% endtab %}
{% tab title="Python" %}
```python
nc = NATS()
await nc.connect(servers=["nats://mytoken@demo.nats.io:4222"])
# Do something with the connection.
```
{% endtab %}
{% tab title="Ruby" %}
```ruby
NATS.start("deadbeef@127.0.0.1:4222") do |nc|
puts "Connected using token!"
end
```
{% endtab %}
{% tab title="TypeScript" %}
```typescript
let url = `nats://:mytoken!@127.0.0.1:${port}`;
let nc = await connect({url: url});
```
{% endtab %}
{% endtabs %}