-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1598eaf
commit 1818e42
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
examples/azuread-service-principal-authtoken/service_principal_authtoken.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"flag" | ||
"fmt" | ||
"log" | ||
|
||
mssql "github.com/denisenkom/go-mssqldb" | ||
) | ||
|
||
var ( | ||
debug = flag.Bool("debug", true, "enable debugging") | ||
password = flag.String("password", "", "the client secret for the app/client ID") | ||
port *int = flag.Int("port", 1433, "the database port") | ||
server = flag.String("server", "", "the database server") | ||
database = flag.String("database", "", "the database name") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if *debug { | ||
fmt.Printf(" password:%s\n", *password) | ||
fmt.Printf(" port:%d\n", *port) | ||
fmt.Printf(" server:%s\n", *server) | ||
fmt.Printf(" database:%s\n", *database) | ||
} | ||
|
||
connString := fmt.Sprintf("server=%s;password=%s;port=%d;database=%s;fedauth=ActiveDirectoryServicePrincipalAccessToken;", *server, *password, *port, *database) | ||
if *debug { | ||
fmt.Printf(" connString:%s\n", connString) | ||
} | ||
|
||
tokenProviderWithCtx := func(ctx context.Context) (string, error) { | ||
return "access_token", nil | ||
} | ||
|
||
connector, err := mssql.NewConnectorWithAccessTokenProvider(connString, tokenProviderWithCtx) | ||
conn := sql.OpenDB(connector) | ||
|
||
//conn, err := sql.Open(azuread.DriverName, connString) | ||
if err != nil { | ||
log.Fatal("Open connection failed:", err.Error()) | ||
} | ||
defer conn.Close() | ||
|
||
stmt, err := conn.Prepare("select 1, 'abc'") | ||
if err != nil { | ||
log.Fatal("Prepare failed:", err.Error()) | ||
} | ||
defer stmt.Close() | ||
|
||
row := stmt.QueryRow() | ||
var somenumber int64 | ||
var somechars string | ||
err = row.Scan(&somenumber, &somechars) | ||
if err != nil { | ||
log.Fatal("Scan failed:", err.Error()) | ||
} | ||
fmt.Printf("somenumber:%d\n", somenumber) | ||
fmt.Printf("somechars:%s\n", somechars) | ||
|
||
fmt.Printf("bye\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters