SoftLayer_User_Customer_ApiAuthentication::getUser failed with Exception - api

package main
import (
"fmt"
"github.com/softlayer/softlayer-go/services"
"github.com/softlayer/softlayer-go/session"
)
func main() {
// SoftLayer API username and key
username := "my-username" // used actual username and api-key
apikey := "My-APIkey"
// Create SoftLayer API session
sess := session.New(username, apikey)
// Get SoftLayer_Account service
resp := services.GetUserCustomerApiAuthenticationService(sess)
users, err := resp.GetUser()
if err != nil {
fmt.Printf("\n Unable to get users:\n - %s\n", err)
return
}
fmt.Printf("\n Unable to get users:\n - %s\n", users)
}
=====
$ ./list_user
Unable to get users:
- SoftLayer_Exception: Object does not exist to execute method on. (SoftLayer_User_Customer_ApiAuthentication::getUser) (HTTP 500)
I tried the similar code to get object as well. It throws same exception.

The reason you are getting this error is due to that both methods need to use an User_Customer_ApiAuthentication identifier.
You can use the following code on both methods as they will use the same authentication id:
package main
import (
"fmt"
"encoding/json"
"github.com/softlayer/softlayer-go/services"
"github.com/softlayer/softlayer-go/session"
)
func main() {
// SoftLayer API username and key
username := "my-username" // used actual username and api-key
apikey := "My-ApiKey"
// Create SoftLayer API session
sess := session.New(username, apikey)
// Get SoftLayer_Account service
resp := services.GetUserCustomerApiAuthenticationService(sess)
users, err := resp.Id(123456).GetUser()
if err != nil {
fmt.Printf("\n Unable to get users:\n - %s\n", err)
return
}
//Following helps to print the result in json format.
jsonFormat, jsonErr := json.MarshalIndent(users,""," ")
if jsonErr != nil {
fmt.Println(jsonErr)
return
}
fmt.Println(string(jsonFormat))
}
If you need to retrieve the authentication id above, you can use the SoftLayer_User_Customer::getObject method and search for the apiAuthenticationKeys relational property that will contain it.

Related

How do i access the Access Token from the spotify web api in Go?

I need to access my access token from the CompleteAuth function, so i can use in my other endpoints.
func CompleteAuth(w http.ResponseWriter, r *http.Request) {
tok, err := auth.Token(r.Context(), state, r)
if err != nil {
http.Error(w, "Couldn't get token", http.StatusForbidden)
log.Fatal(err)
}
if st := r.FormValue("state"); st != state {
http.NotFound(w, r)
log.Fatalf("State mismatch: %s != %s\n", st, state)
}
// use the token to get an authenticated client
client := spotify.New(auth.Client(r.Context(), tok))
fmt.Println("Access Token:" + tok.AccessToken + "\n")
fmt.Fprintf(w, "Login Completed!")
ch <- client
}
func RegHand(w http.ResponseWriter, r *http.Request) {
log.Println("Got request for:", r.URL.String())
url := auth.AuthURL(state)
fmt.Println("Please log in to Spotify by visiting the following page in your browser:", url+"\n")
// wait for auth to complete
client := <-ch
// use the client to make calls that require authorization
user, err := client.CurrentUser(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("You are logged in as:", user.ID)
}
I've triend to create a pointer to the tok variable, but i does not seem to work.
Would it be possible to create a custom interface that contains the acces token that is generated by the completeAuth function.

Golang - Sending API POST Request - Not enough arguments error

The following code attempts to send a POST API request with a payload that is in RequestDetails.FormData. When I run main.go function, then I get the following errors.
go run main.go
# command-line-arguments
./main.go:53:17: not enough arguments in call to http.HandleFunc
./main.go:53:33: not enough arguments in call to reqDetails.Send
have ()
want (http.ResponseWriter, *http.Request)
./main.go:53:33: reqDetails.Send() used as value
The code is available below. Anybody knows what I could do wrong here? Thanks a lot for your help.
//main.go
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
// RequestDetails contains input data for Send
type RequestDetails struct {
EndPoint string
FormType string
FormData map[string]string
}
// Send sends API POST request to an endpoint
func (rd RequestDetails) Send(w http.ResponseWriter, r *http.Request) {
json_data, err := json.Marshal(rd.FormData)
if err != nil {
log.Fatal(err)
}
resp, err := http.Post(rd.EndPoint, rd.FormType, bytes.NewBuffer(json_data))
if err != nil {
log.Fatal(err)
}
fmt.Println(resp)
}
func main() {
m := map[string]string{
"AuthParamOne": "AP0000001",
"AuthParamTwo": "AP0000002",
"AuthParamThree": "AP0000003",
}
reqDetails := RequestDetails{
EndPoint: "https://httpbin.org/post",
FormType: "application/json",
FormData: m,
}
http.HandleFunc(reqDetails.Send())
}
you have to use HandleFunc in following below:
func HandleFunc(pattern string, handler func(ResponseWriter, *Request))
for code above follow this:
http.HandleFunc("/test",reqDetails.Send) //-> add reference instead of calling 'reqDetails.Send()'
reference: https://pkg.go.dev/net/http#HandleFunc
please vote up :)
In your Send method, you don't make use of w http.ResponseWriter, r *http.Request, So it seems you don't need them:
func (rd RequestDetails) Send() {...
Also in your last line, HandleFunc requires different arguments which once again is not necessary in your case. Just try to run the Send method:
reqDetails.Send()
The whole main.go file:
//main.go
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
// RequestDetails contains input data for Send
type RequestDetails struct {
EndPoint string
FormType string
FormData map[string]string
}
// Send sends API POST request to an endpoint
func (rd RequestDetails) Send() {
json_data, err := json.Marshal(rd.FormData)
if err != nil {
log.Fatal(err)
}
resp, err := http.Post(rd.EndPoint, rd.FormType, bytes.NewBuffer(json_data))
if err != nil {
log.Fatal(err)
}
fmt.Println(resp)
}
func main() {
m := map[string]string{
"AuthParamOne": "AP0000001",
"AuthParamTwo": "AP0000002",
"AuthParamThree": "AP0000003",
}
reqDetails := RequestDetails{
EndPoint: "https://httpbin.org/post",
FormType: "application/json",
FormData: m,
}
reqDetails.Send()
}
if your code like this
watcher := bufio.NewReader(os.Stdin)
input, _ := watcher.ReadString()
fmt.Println(input)
you needed this for reading line line
old -> input, _ := watcher.ReadString()
new -> input, _ := watcher.ReadString('\n')

Rest API in Go (Consume and host)

I am trying to request something, like a book by its id, and then host it locally so that if I write my local URL, like http://localhost:8080​/books?books=<book-id> it would show me the specific result.
To try to be concrete, I need to connect the two. Get the information from that URL, so "consume" and also host it locally, specifically by ID. I am not sure how to do both at once.
To create the paths, I've been using gorilla mux
So separately, I've used this, which would give me all the books at once (URL is not real).
func main() {
response, err := http.Get("https://bookibook.herokuapp.com/books/")
if err != nil {
fmt.Printf("there is no book with this ID %s\n", err)
} else {
data, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(data))
}
}
and then this, which would create a local path for http://localhost:8080/books/ID
import (
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
)
func getID(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
fmt.Fprintf(w, "Get id %s\n", vars["id"])
}
func main() {
// Configure routes.
router := mux.NewRouter()
router.HandleFunc("/books/{id}/", getID).Methods(http.MethodGet)
// Start HTTP server.
if err := http.ListenAndServe(":8080", router); err != nil {
log.Fatal(err)
}
}

How to use Golang's github.com/google/google-api-go-client/customsearch/v1

I've done the Oauth callback from which people said it's not needed, and just needs the cx code but I have yet to figure out how to add the cx parameter to the call.
package main
import (
"fmt"
"log"
"github.com/vinniyo/authCallback"
"github.com/google/google-api-go-client/customsearch/v1"
)
func main() {
client, err := authCallback.BuildOAuthHTTPClient()
if err != nil {
log.Fatalf("Error building OAuth client: %v", err)
}
service, err := customsearch.New(client)
if err != nil {
log.Fatalf("Error creating YouTube client: %v", err)
}
fmt.Println(service.Cse.List("bob").Do())
}
I know to upload a video to youtube you add parameters before do() but how do you figure out the formatting? eg:
upload := &youtube.Query{
Status: &youtube.VideoStatus{PrivacyStatus: *privacy},
}
The CseListCall struct has Cx method which lets you add that parameter: https://godoc.org/google.golang.org/api/customsearch/v1#CseListCall
fmt.Println(service.Cse.List("bob").Cx("my_cx_id").Do())

Golang imap.DialTLS Config example

I used to be able to connect to port 143 of a mail server like this:
c, err := imap.Dial(mailServer)
The code above connects to port 143 of the mailServer. Now I have a new mail server only accepts port 993. Looking at the Golang imap source code, the function DialTLS will connect to port 993. The signature of DialTLS looks like this:
func DialTLS(addr string, config *tls.Config) (c *Client, err error)
Now I don't know how to construct the *tls.Config. I Googled around, but didn't not find anything really useful. Can someone show me some example how to construct the *tls.Config?
I tried to pass in nil as the second parameter, it compiles, and I didn't get any runtime error. But it seems no new mails were fetched, even I believe there should be.
My fetch mail code looks like this:
// testimap
package main
import (
"bytes"
"code.google.com/p/go-imap/go1/imap"
"fmt"
"net/mail"
"time"
)
type Mail struct {
Subject string
Body string
From string
Uid uint32
}
func FetchMail(lastUid uint32) []*Mail {
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
}
}()
//
// Note: most of error handling code is omitted for brevity
//
var (
c *imap.Client
cmd *imap.Command
rsp *imap.Response
)
// Connect to the server
c, err := imap.DialTLS(mailServer, nil)
if err != nil {
fmt.Println(err)
}
// Remember to log out and close the connection when finished
defer c.Logout(30 * time.Second)
// Print server greeting (first response in the unilateral server data queue)
//fmt.Println("Server says hello:", c.Data[0].Info)
c.Data = nil
// Enable encryption, if supported by the server
if c.Caps["STARTTLS"] {
c.StartTLS(nil)
}
// Authenticate
if c.State() == imap.Login {
c.Login(mailSupportUser, mailSupportPw)
}
//// List all top-level mailboxes, wait for the command to finish
cmd, err = imap.Wait(c.List("", "%"))
if err != nil {
fmt.Println(err)
}
// Print mailbox information
//fmt.Println("\nTop-level mailboxes:")
//for _, rsp = range cmd.Data {
// fmt.Println("|--", rsp.MailboxInfo())
//}
// Check for new unilateral server data responses
//for _, rsp = range c.Data {
// fmt.Println("Server data:", rsp)
//}
c.Data = nil
// Open a mailbox (synchronous command - no need for imap.Wait)
c.Select("INBOX", true)
//fmt.Print("\nMailbox status:\n", c.Mailbox)
// Fetch the headers of the 10 most recent messages
set, err := imap.NewSeqSet(fmt.Sprint(lastUid, ":*"))
if err != nil {
fmt.Println(err)
}
//if c.Mailbox.Messages >= 10 {
// set.AddRange(c.Mailbox.Messages-9, c.Mailbox.Messages)
//} else {
// set.Add("1:*")
//}
cmd, err = c.UIDFetch(set, "RFC822.HEADER", "RFC822.TEXT")
if err != nil {
fmt.Println(err)
}
// Process responses while the command is running
//fmt.Println("\nMost recent messages:")
mails := make([]*Mail, 0, 10)
for cmd.InProgress() {
// Wait for the next response (no timeout)
c.Recv(-1)
// Process command data
for _, rsp = range cmd.Data {
if err != nil {
fmt.Println(err)
}
header := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.HEADER"])
uid := imap.AsNumber((rsp.MessageInfo().Attrs["UID"]))
body := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.TEXT"])
if msg, err := mail.ReadMessage(bytes.NewReader(header)); msg != nil {
if err != nil {
fmt.Println(err)
}
mail := &Mail{
Subject: msg.Header.Get("Subject"),
From: msg.Header.Get("FROM"),
Body: string(body),
Uid: uid,
}
if mail.Uid < lastUid {
continue
}
mails = append(mails, mail)
}
}
cmd.Data = nil
c.Data = nil
}
// Check command completion status
if rsp, err := cmd.Result(imap.OK); err != nil {
if err == imap.ErrAborted {
fmt.Println("Fetch command aborted")
} else {
fmt.Println("Fetch error:", rsp.Info)
}
}
fmt.Println(mails)
return mails
}
First off, you should be using the project's GitHub repo as the Google Code project stated development was moving to GitHub due to Google Code shutting down. It's a few commits ahead of the Google Code repo too, so you won't be getting any updates if you don't migrate to the GitHub repo.
Secondly, looking at the package's demo, passing nil to DialTLS as the TLS client seems to be the proper way to connect with a default TLS client.
From the information you've given, it seems like it may be an issue with your server accepting connections over that port. I would look into if the port is open to the client you're trying to connect from or if your IMAP server is even accepting TLS connections.
If you're absolutely sure it's not a server issue after further debugging, I would file an issue on the project's GitHub issue tracker to get help from people who are more familiar with the package, seeing that it's a third party package.