gourlhttp-redirectmedium.com-publishing-api

Open a url in golang and read the redirected url


I am writing a command line tool for accessing Medium.com's API using Go. They have released this SDK for golang too.

I have followed the instructions until the point where I can build a url using secret state, redirect url, and scopes.

package main

import (
    "fmt"
    "net/http"

    "github.com/Medium/medium-sdk-go"
)

func check(e error) {
    if e != nil {
        panic(e)
    }
}

func mediumAuth() {
    m := medium.NewClient("clientIDString", "clientSecretString")

    url := m.GetAuthorizationURL("supersecretstring", "http://hasit.me/callback/medium", medium.ScopeBasicProfile, medium.ScopePublishPost)

    //next step
}


func main() {
    mediumAuth()
}

The next step is to open the url to let user give permission for accessing his/her profile. I can successfully open the url.

exec.Command("open", url).Start()

Now, once the user clicks on the url, how can I fetch the redirected url? I even tried using http GET but the response is not the one that I want.

resp, err := http.Get(url)

I am sorry if the answer is obvious but this will be my first program where I use some API in any language. Help will be appreciated.


Solution

  • The important shift that needs to happen is around your question how can I fetch the redirected url, because the answer is: You don't. Medium is going to post to this URL. You need to have a webserver somewhere that listens on this URL and parses the response.

    Since you're writing a CLI tool and not a web service, this is probably the inappropriate way to do authentication: Medium provides Self-Issued Access Tokens for use in desktop applications.