Browse Source

added request_token method and updated readme

Kevin Hughes 11 years ago
parent
commit
8f195c2763
2 changed files with 32 additions and 19 deletions
  1. 14 12
      README.rdoc
  2. 18 7
      lib/shopify_api/session.rb

+ 14 - 12
README.rdoc

@@ -30,7 +30,7 @@ ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveR
 
 
 1. First create a new application in either the partners admin or your store admin. For a private App you'll need the API_KEY and the PASSWORD otherwise you'll need the API_KEY and SHARED_SECRET.
 1. First create a new application in either the partners admin or your store admin. For a private App you'll need the API_KEY and the PASSWORD otherwise you'll need the API_KEY and SHARED_SECRET.
 
 
-2. For a private App you just need to set the base site url as follows (where hostname is your site) 
+2. For a private App you just need to set the base site url as follows:
     
     
     shop_url = "https://#{API_KEY}:#{PASSWORD}@SHOP_NAME.myshopify.com/admin"
     shop_url = "https://#{API_KEY}:#{PASSWORD}@SHOP_NAME.myshopify.com/admin"
     ShopifyAPI::Base.site = shop_url
     ShopifyAPI::Base.site = shop_url
@@ -41,7 +41,7 @@ ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveR
 
 
     ShopifyAPI::Session.setup({:api_key => API_KEY, :secret => SHARED_SECRET})
     ShopifyAPI::Session.setup({:api_key => API_KEY, :secret => SHARED_SECRET})
 
 
-3. To access a shop's data apps need an access token from that specific shop. This is a two-stage process. Before interacting with a shop for the first time an app should redirect the user to the following URL:
+3. In order to access a shop's data, apps need an access token from that specific shop. This is a two-stage process. Before interacting with a shop for the first time an app should redirect the user to the following URL:
 
 
     GET https://SHOP_NAME.myshopify.com/admin/oauth/authorize
     GET https://SHOP_NAME.myshopify.com/admin/oauth/authorize
 
 
@@ -66,19 +66,21 @@ ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveR
 
 
    and you'll get your permanent access token back in the response.
    and you'll get your permanent access token back in the response.
 
 
-   There is also a method to create this url for you:
-    auth_url = ShopifyAPI::Session.create_auth_url("SHOP_NAME.myshopify.com", code)
+   There is a method to make the request and get the token for you, first instantiate a new session with the shop url:
 
 
-5. Use that token to instantiate a session that is ready to make calls to the given shop.
+    session = ShopifyAPI::Session.new("SHOP_NAME.myshopify.com")
+   
+   Then call:
+
+    token = session.request_token(code)
 
 
-    session = ShopifyAPI::Session.new("SHOP_NAME.myshopify.com", token)
-    session.valid?  # returns true
+   Which will request the token, save it to the session object and return it.
 
 
-6. Now you can activate the session and you're set:
+5. The session must be activated before use:
 
 
     ShopifyAPI::Base.activate_session(session)
     ShopifyAPI::Base.activate_session(session)
 
 
-7. Start making authorized API requests for that shop. Data is returned as ActiveResource instances:
+6. Now you're ready to make authorized API requests to your shop! Data is returned as ActiveResource instances:
 
 
     shop = ShopifyAPI::Shop.current
     shop = ShopifyAPI::Shop.current
 
 
@@ -100,7 +102,7 @@ ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveR
 
 
     products = ShopifyAPI::Session.temp("SHOP_NAME.myshopify.com", token) { ShopifyAPI::Product.find(:all) }
     products = ShopifyAPI::Session.temp("SHOP_NAME.myshopify.com", token) { ShopifyAPI::Product.find(:all) }
 
 
-8. Finally, you can also clear the session (for example if you want to work with another shop):
+8. If you want to work with another shop, you'll first need to clear the session::
 
 
     ShopifyAPI::Base.clear_session
     ShopifyAPI::Base.clear_session
 
 
@@ -111,7 +113,7 @@ This package also includes the `shopify` executable to make it easy to open up a
 
 
 1. Obtain a private API key and password to use with your shop (step 2 in "Getting Started")
 1. Obtain a private API key and password to use with your shop (step 2 in "Getting Started")
 
 
-2. Use the `shopify` script to save the credentials for the shop to quickly login.
+2. Use the `shopify` script to save the credentials for the shop to quickly log in.
 
 
     shopify add yourshopname
     shopify add yourshopname
 
 
@@ -121,7 +123,7 @@ This package also includes the `shopify` executable to make it easy to open up a
 
 
     shopify console
     shopify console
 
 
-4. Enter the following for the full list of the commands.
+4. To see the full list of commands, type:
 
 
     shopify help
     shopify help
 
 

+ 18 - 7
lib/shopify_api/session.rb

@@ -45,12 +45,6 @@ module ShopifyAPI
         "#{self.protocol}://#{shop_url}/admin/oauth/authorize?#{parameterize(params)}"
         "#{self.protocol}://#{shop_url}/admin/oauth/authorize?#{parameterize(params)}"
       end
       end
 
 
-      def create_auth_url(shop_url, code)
-        self.prepare_url(shop_url)
-        params = {:client_id => self.api_key, :client_secret => self.secret, :code => code}
-        "#{self.protocol}://#{shop_url}/admin/oauth/access_token?#{parameterize(params)}"
-      end
-
       def parameterize(params)
       def parameterize(params)
         URI.escape(params.collect{|k,v| "#{k}=#{v}"}.join('&'))
         URI.escape(params.collect{|k,v| "#{k}=#{v}"}.join('&'))
       end
       end
@@ -85,7 +79,24 @@ module ShopifyAPI
         end
         end
       end
       end
     end
     end
-    
+
+    def request_token(code)
+      return self.token if self.token
+      
+      uri = URI.parse("#{self.protocol}://#{self.url}/admin/oauth/access_token")      
+      https = Net::HTTP.new(uri.host, uri.port)
+      https.use_ssl = true
+      request = Net::HTTP::Post.new(uri.request_uri)
+      request.set_form_data({"client_id" => self.api_key, "client_secret" => self.secret, "code" => code})
+      response = https.request(request)
+
+      if response.code == "200"
+        self.token = JSON.parse(response.body)['access_token']
+      else
+        raise response.msg
+      end
+    end
+
     def shop
     def shop
       Shop.current
       Shop.current
     end
     end