Coder Social home page Coder Social logo

pinterest-api's Introduction

Pinterest

Gem Version
Code Climate

This is the Ruby gem for interacting with the official Pinterest REST API.

This gem uses Faraday and Hashie to make requests and parse the responses.

Battle-tested at Shopseen to help merchants sell more.

Usage

Obtain an access token from Pinterest. You can generate one here.

$ gem install pinterest-api

require 'pinterest-api'

client = Pinterest::Client.new(ACCESS_TOKEN)

# Get the authenticated user's Pinterest account info
client.me

# Get the pins that the authenticated user likes
client.get_likes

# Get the authenticated user's followers
client.get_followers

# Get the boards that the authenticated user follows
client.get_followed_boards

# Get the Pinterest users that the authenticated user follows
client.get_followed_users

# Get the interests that the authenticated user follows
client.get_followed_interests

# Follow a user
client.follow_user('shopseen')

# Unfollow a user
client.unfollow_user('shopseen')

# Follow a board
client.follow_board(<board_id>)

# Unfollow a board
client.unfollow_board(<board_id>)

# Follow an interest
> This endpoint is no longer part of the Pinterest documentation, and has always returned an error
client.follow_interest(<interest_id>)

# Unfollow an interest
> This endpoint is no longer part of the Pinterest documentation, and has always returned an error
client.unfollow_interest(<interest_id>)

# Get all of the authenticated users's pins
client.get_pins  

# Get all of the authenticated users's boards
client.get_boards

# Search for the authenticated users's pins related to shoes
client.get_pins(query: 'shoes')  

# Search for the authenticated users's boards related to shoes
client.get_boards(query: 'shoes')

# Get the account info for a Pinterest user
client.get_user('<username>')

Creating pins

You can create pins as follows

@client.create_pin({
  board: '<username>/<board_name>' OR '<board_id>',
  note: 'My note'
  link: 'https://www.google.com',
  image_url: 'http://marketingland.com/wp-content/ml-loads/2014/07/pinterest-logo-white-1920.png'
})

You can also upload your own image file like so

@client.create_pin({
  board: '1154178055932271277',
  note: 'Test from ruby gem',
  link: 'https://www.shopseen.com',
  image: Faraday::UploadIO.new(your_file_path, "image/<image_type>")
})

Authentication

You can generate access tokens through the link above, or you can use OAuth Authentication

In your app, make sure you have the omniauth gem installed. Add the following to your intializers/omniauth.rb file

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :pinterest, ENV['PINTEREST_APP_ID'], ENV['PINTEREST_APP_SECRET']
end

Direct your users to /auth/pinterest

Once they approve your app, they'll be redirected to your callback URL, which should be something like auth/pinterest/callback with a hash of OAuth values from Pinterest in request.env['omniauth.auth']

For more details, check out "Integrating OmniAuth Into Your Application"
https://github.com/intridea/omniauth

Request options

You can set any request options that are valid in Faraday::Connection by adding them as a hash to the Pinterest Client initializer.

Example:

  client = Pinterest::Client.new(ACCESS_TOKEN, {
    request: {
      timeout: 1.5,
      open_timeout: 1,
    }
  })

  counts = client.get_user('<username>', {fields: "counts"})

Known Issues

The gem is currently under active development. The following issues cause the test specs to fail, though it's not clear to me that these issues are not with the Pinterest API itself.

  • PATCH requests not working, endpoint path is not being appended to base
  • POST/DELETE requests for following/unfollowing interests respectively are not working

TODO

  • Pagination
  • document Mash response methods

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/realadeel/pinterest-api.
Please provide a failing test for bug reports, and a passing test for pull requests.

pinterest-api's People

Contributors

danabrit avatar justin-rhoades avatar realadeel avatar shmargum avatar sts10 avatar vishalzambre avatar westonplatter 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

Watchers

 avatar  avatar  avatar  avatar

pinterest-api's Issues

Access response http status

For proper error handling we want to rely on the http status codes which pinterest api gives back: https://developers.pinterest.com/docs/api/overview/#errors

Currently in https://github.com/realadeel/pinterest-api/blob/master/lib/pinterest/client.rb#L60 only the response body will be returned.

An example response:

[2] pry(#<Onlim::Adapter::Publish::Link::Pinterest>)> response
=> {"status"=>"failure", "code"=>3, "host"=>"devplatform-devapi-prod-f7410648", "generated_at"=>"Wed, 26 Oct 2016 09:19:28 +0000", "message"=>"Authorization failed.", "data"=>nil}

We want to rely on the documented http return status codes instead of relying on the code which is returned in the body (didn't find documentation about this code).

Are there any problems in returning the whole response?

oauth issue

Hi @realadeel
i want to get authorization code without popups to pinterest.com site . because in my network cannot visit pinterest.com host . but i have a server can visit that host .

so do you have any way to help me resolve this issue?

thanks .

How to use return form get_boards?

When I issue a request to the gem to get my private boards with get_boards method I get the strange answer:

{"data"=>[],
 "page"=>
  {"cursor"=>"b28yNXxmMGVmZDA5YjB5Yjk5NDIxZjgzODZjNDRkZWEzNTRhZmIyZDRjZjE3ZWY4YmRmMWE4NmVhZGQwNDg1NTNmOTAw",
   "next"=>
    "https://api.pinterest.com/v1/me/search/pins/?access_token=ATn4phEFgj31x3eaA1tgFLeknE7MFQ0LI9ZTHe5Epzcd5oAzhAAAAAA&query=Agnetis&cursor=b28yNXxmMGVmZDA5YjA5Yjk5NDIxZjgzODZjNDRkZWEzNTRhZmIyZDRjZjE3ZWY4YmRmMWE4NmVhZGQwNDg1NTNmOTAw"}}

The documentation https://developers.pinterest.com/docs/api/boards/ give not the same answer as I get from the method above.

So the question is how to use the method properly?

Error You need to upload an image or provide the 'image_url' parameter on create_pin

I have the following rake task to pin images to Pinterest

namespace :pinterest_product_images do
  desc 'pin product iamges to pinterest'
  task pin: :environment do
    client = Pinterest::Client.new(PINTEREST_CONFIG[:access_token])

    Spree::Product.find_each(conditions: { published: true }) do |product|
      product_image = product.images.first.try(:attachment)
      if product_image
        file_path = File.expand_path('') + "/#{product_image.path(:original)}"
        content_type = product_image.content_type
        if file_path && content_type
          resp = client.create_pin({ board: '<my board id>',
                                     note: "#{product.name} MyApp #{product.category}",
                                     link: "#{PINTEREST_CONFIG[:host]}/products/#{product.slug}",
                                     image: Faraday::UploadIO.new(file_path, content_type)
                                   })
          puts "path: #{file_path}, type: #{content_type}  => #{resp[:message]}" if resp[:message]
          Rails.logger.info "Pinterest Response: #{resp}"
        end
      end
    end
  end
end

I am getting the following output

path: /home/my-name/Desktop/my-site/public/my-assets/products/4/original/sample1.jpg, type: image/jpeg  => You need to upload an image or provide the 'image_url' parameter

path: /home/my-name/Desktop/my-site/public/my-assets/products/1/original/sample2.jpg, type: image/jpeg  => You need to upload an image or provide the 'image_url' parameter

path: /home/my-name/Desktop/my-site/public/my-assets/products/3/original/sample3.jpg, type: image/jpeg  => You need to upload an image or provide the 'image_url' parameter

path: /home/my-name/Desktop/my-site/public/my-assets/products/2/original/sample4.JPG, type: image/jpeg  => You need to upload an image or provide the 'image_url' parameter

path: /home/my-name/Desktop/my-site/public/my-assets/products/1/original/sample5.jpg, type: image/jpeg  => You need to upload an image or provide the 'image_url' parameter

$ ruby -v
ruby 2.1.8p440 (2015-12-16 revision 53160) [x86_64-linux]
$ rails -v
Rails 4.0.13

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.