当前位置: 首页>>代码示例>>Golang>>正文


Golang OAuth2Client.ExchangeRequestTokenForAccess方法代码示例

本文整理汇总了Golang中github.com/pomack/oauth2_client/go/oauth2_client.OAuth2Client.ExchangeRequestTokenForAccess方法的典型用法代码示例。如果您正苦于以下问题:Golang OAuth2Client.ExchangeRequestTokenForAccess方法的具体用法?Golang OAuth2Client.ExchangeRequestTokenForAccess怎么用?Golang OAuth2Client.ExchangeRequestTokenForAccess使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/pomack/oauth2_client/go/oauth2_client.OAuth2Client的用法示例。


在下文中一共展示了OAuth2Client.ExchangeRequestTokenForAccess方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: HandleClientAccept

func HandleClientAccept(w http.ResponseWriter, req *http.Request) {
	var c oauth2_client.OAuth2Client = nil
	method := "GET"
	headers := make(http.Header)
	uri := ""
	query := make(url.Values)
	var reader io.Reader = nil
	props := getProperties()
	q := req.URL.Query()
	oauth2_client.LogInfo("=================================")
	oauth2_client.LogInfo("Received request from User: ")
	reqBytes, _ := httputil.DumpRequest(req, true)
	oauth2_client.LogInfo(string(reqBytes))
	oauth2_client.LogInfo("=================================")
	var useTemplate *template.Template = nil
	var useTemplateData interface{} = nil
	if site := q.Get("site"); len(site) > 0 {
		if index := strings.Index(site, "?"); index >= 0 {
			site = site[0:index]
		}
		m := make(map[string]interface{})
		switch site {
		case "facebook.com":
			c = NewFacebookOauth2ClientTester(props)
			uri = props.GetAsString("facebook.client.test_url")
			useTemplate = PARSED_FACEBOOK_TEMPLATE
		case "google.com":
			c = NewGoogleOauth2ClientTester(props)
			uri = props.GetAsString("google.client.test_url")
			useTemplate = PARSED_GOOGLE_TEMPLATE
		case "plus.google.com":
			c = NewGooglePlusOauth2ClientTester(props)
			uri = props.GetAsString("googleplus.client.test_url")
			useTemplate = PARSED_GOOGLEPLUS_TEMPLATE
		case "linkedin.com":
			c = NewLinkedInOauth2ClientTester(props)
			uri = props.GetAsString("linkedin.client.test_url")
			useTemplate = PARSED_LINKEDIN_TEMPLATE
		case "smugmug.com":
			// smugmug doesn't support query strings properly
			newRawUrl := strings.Replace(req.URL.String(), "site=smugmug.com?", "site=smugmug.com&", 1)
			newUrl, _ := url.Parse(newRawUrl)
			if newUrl != nil {
				req.URL = newUrl
				q = newUrl.Query()
			}
			c = NewSmugMugOauth2ClientTester(props)
			uri = props.GetAsString("smugmug.client.test_url")
			useTemplate = PARSED_SMUGMUG_TEMPLATE
		case "twitter.com":
			c = NewTwitterOauth2ClientTester(props)
			uri = props.GetAsString("twitter.client.test_url")
			useTemplate = PARSED_TWITTER_TEMPLATE
		case "yahoo.com":
			c = NewYahooOauth2ClientTester(props)
			uri = props.GetAsString("yahoo.client.test_url")
			useTemplate = PARSED_YAHOO_TEMPLATE
		default:
			log.Fatal("Unable to determine OAuth client to handle response: ", req.URL.String())
		}
		m["c"] = c
		m["url"] = uri
		m["output"] = ""
		useTemplateData = m
	} else {
		log.Fatal("Unable to determine OAuth client to handle response: ", req.URL.String())
	}
	err := c.ExchangeRequestTokenForAccess(req)
	if err != nil {
		w.Header().Set("Content-Type", "text/plain")
		w.WriteHeader(500)
		io.WriteString(w, "Error exchanging request token for access token\n\n")
		io.WriteString(w, err.Error())
		return
	}
	if useTemplate != nil {
		w.Header().Set("Content-Type", "text/html")
		w.WriteHeader(200)
		err = useTemplate.Execute(w, useTemplateData)
		if err != nil {
			oauth2_client.LogErrorf("Error: %T %v", err, err)
		}
	} else {
		oauth2_client.LogInfo("Retrieving User Info...")
		userInfo, err3 := c.RetrieveUserInfo()
		oauth2_client.LogInfof("UserInfo: %T %v", userInfo, userInfo)
		oauth2_client.LogInfof("Error: %T %v", err3, err3)

		r, _, err2 := oauth2_client.AuthorizedRequest(c, method, headers, uri, query, reader)
		if err2 != nil {
			w.Header().Set("Content-Type", "text/plain")
			w.WriteHeader(500)
			io.WriteString(w, "Error retrieving authorized response for "+uri+"?"+query.Encode()+"\n\n")
			io.WriteString(w, err.Error())
			return
		}
		h := w.Header()
		for k, v := range r.Header {
			for _, v1 := range v {
				h.Add(k, v1)
//.........这里部分代码省略.........
开发者ID:pomack,项目名称:oauth2_client.go,代码行数:101,代码来源:oauth2_client_tester.go


注:本文中的github.com/pomack/oauth2_client/go/oauth2_client.OAuth2Client.ExchangeRequestTokenForAccess方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。