Coder Social home page Coder Social logo

medium-sdk-go's Introduction

Medium SDK for Go

This repository contains the open source SDK for integrating Medium's OAuth2 API into your Go app.

Install

go get github.com/Medium/medium-sdk-go

Usage

Create a client, then call commands on it.

package main

import (
	medium "github.com/medium/medium-sdk-go"
	"log"
)

func main() {
	// Go to https://medium.com/me/applications to get your applicationId and applicationSecret.
	m := medium.NewClient("YOUR_APPLICATION_ID", "YOUR_APPLICATION_SECRET")

	// Build the URL where you can send the user to obtain an authorization code.
	url := m.GetAuthorizationURL("secretstate", "https://yoursite.com/callback/medium",
        medium.ScopeBasicProfile, medium.ScopePublishPost)

	// (Send the user to the authorization URL to obtain an authorization code.)

	// Exchange the authorization code for an access token.
	at, err := m.ExchangeAuthorizationCode("YOUR_AUTHORIZATION_CODE", "https://yoursite.com/callback/medium")
	if err != nil {
		log.Fatal(err)
	}

	// The access token is automatically set on the client for you after
	// a successful exchange, but if you already have a token, you can set it
	// directly.
	m.AccessToken = at.AccessToken

	// If you have a self-issued access token, you can skip these steps and
	// create a new client directly:
	m2 := medium.NewClientWithAccessToken("SELF_ISSUED_ACCESS_TOKEN")

	// Get profile details of the user identified by the access token.
	// Empty string mean current user, otherwise you need to indicate
	// the user id (alphanumeric string with 65 chars)
	u, err := m2.GetUser("")
	if err != nil {
		log.Fatal(err)
	}

	// Create a draft post.
	p, err := m.CreatePost(medium.CreatePostOptions{
		UserID:        u.ID,
		Title:         "Title",
		Content:       "<h2>Title</h2><p>Content</p>",
		ContentFormat: medium.ContentFormatHTML,
		PublishStatus: medium.PublishStatusDraft,
	})
	if err != nil {
		log.Fatal(err)
	}

	// When your access token expires, use the refresh token to get a new one.
	nt, err := m.ExchangeRefreshToken(at.RefreshToken)
	if err != nil {
		log.Fatal(err)
	}

	// Confirm everything went ok. p.URL has the location of the created post.
	log.Println(url, at, u, p, nt)
}

Contributing

Questions, comments, bug reports, and pull requests are all welcomed. If you haven't contributed to a Medium project before please head over to the Open Source Project and fill out an OCLA (it should be pretty painless).

Authors

Jamie Talbot

Dan Pupius

Andrew Bonventre

License

Copyright 2015 A Medium Corporation

Licensed under Apache License Version 2.0. Details in the attached LICENSE file.

medium-sdk-go's People

Contributors

aki237 avatar andybons avatar baris avatar kylehg avatar majelbstoat avatar mavimo avatar njuettner avatar sanatgersappa avatar zchee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

medium-sdk-go's Issues

json: cannot unmarshal array into Go value of type medium.Publications

Executing the code below (access code deleted),

package main

import (
	"fmt"
	"log"

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

func main() {
	m2 := medium.NewClientWithAccessToken("")

	user, err := m2.GetUser("")
	fmt.Printf("User is %q\n", user)
	pubs, err := m2.GetUserPublications(user.ID)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("No. of publications: %d\n", len(pubs.Data))
}

leads to:

User is &{"1278b5260445c10f4e13b9f37f4f60e9e59376cbd8e5230bf1efd5f700160f860" "debugjois" "ದೀಪಕ್/दीपक/دیپک/Deepak" "https://medium.com/@debugjois" "https://cdn-images-1.medium.com/fit/c/400/400/1*VY6glnPvt0Zbgi-uplEV0g.jpeg"}
2018/02/07 16:25:19 json: cannot unmarshal array into Go value of type medium.Publications
exit status 1

feat: medium partner program stats

I have multiple analytics sources from several sites, it would be cool to read my stats from the medium partner program. Is this already possible?

Screen Shot 2020-06-09 at 21 01 04

500 error on GetUser API when passing `userId`

I am getting 500 error on passing the userId in GetUser API

user, err := client.GetUser(userId)

Authentication using user-generated access token
SDK version: v0.0.0-20171230201202-4daca056cf6a

Fetch the redirected url

I am writing a command line tool for publishing to Medium. This will be my first program where I access any API using golang.

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)

Help will be appreciated.

API Error: User not found!

Medium API doesn't work.
I have created an APP and authorized my user. Fetched the access_token successfully.
But when I try to fetch an authorized user's info, I have gotten the below error message:
{"errors":[{"message":"User not found","code":6004}]}

These are the logs:
http://prntscr.com/qarjrp
http://prntscr.com/qark4h

I have tried to create a new "Integration tokens" from my User settings and tried to use it instead of the access token. But the same result. I can not get authorized user's information.
What is wrong? Could you help me, please?
I have contacted the Medium support team several times. But their support is very bad. They reply very late and every time writes unrelated messages.

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.