在Go语言中打开一个URL并读取重定向后的URL。

huangapple go评论80阅读模式
英文:

Open a url in golang and read the redirected url

问题

我正在使用Go编写一个命令行工具,用于访问Medium.com的API。他们也发布了适用于golang的SDK。

我已经按照说明进行操作,直到可以使用密钥状态、重定向URL和范围构建URL。

下一步是打开URL,让用户授权访问他/她的个人资料。我可以成功打开URL。

现在,一旦用户点击URL,我该如何获取重定向的URL?我甚至尝试使用http GET,但响应不是我想要的。

对不起,如果答案很明显,但这将是我在任何语言中使用API的第一个程序。感谢您的帮助。

英文:

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.

答案1

得分: 2

需要发生的重要变化是关于你的问题“如何获取重定向的URL”,因为答案是:你不需要获取。Medium将会将帖子发布到该URL上。你需要在某个地方拥有一个监听该URL并解析响应的Web服务器。

由于你正在编写一个命令行工具而不是一个Web服务,这可能不是适当的身份验证方式:Medium为桌面应用程序提供了自签发访问令牌

英文:

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.

huangapple
  • 本文由 发表于 2015年12月27日 03:47:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/34474664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定